Angular中的克隆元素

时间:2015-07-24 13:35:54

标签: angularjs angularjs-scope angularjs-ng-repeat

我从这篇文章中得到了Vladyslav Babenko的代码(Clone elements in angularjs):

var App = angular.module('App', []).controller('Test', ['$scope',
  function($scope) {

    $scope.inputCounter = 0;
    $scope.inputs = [{
      id: 'input'
    }];
    $scope.add = function() {
      $scope.inputTemplate = {
        id: 'input-' + $scope.inputCounter,
        name: ''
      };
      $scope.inputCounter += 1;
      $scope.inputs.push($scope.inputTemplate);
    };
   $scope.teste = function (pergunta) {
    console.log("in");

}
  }
])

<!DOCTYPE html>
<html ng-app="App">

<head lang="en">
  <meta charset="UTF-8">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>

<body ng-controller="Test">
  <form ng-submit="teste()">
    <div class="pure-g entry" ng-repeat="input in inputs track by input['id']">
      <div class="pure-u-1-5">
        <input type="text" class="pure-input-1" id="input" name="input-1">
      </div>
      <div class="pure-u-1-5">
        <input type="text" class="pure-input-1" id="date" name="date">
    </div>
    <div class="pure-u-1-5">`enter code here`
        <input type="text" class="pure-input-1" id="input-2" name="input-2">
    </div>
    </div>
  </form>
  <button type="button" id="add" ng-click="add()">Add</button>
  <button type="submit">teste</button>
</body>

</html>

它工作正常,但是当添加倍数形式时,我如何从每个表单中获取值?

1 个答案:

答案 0 :(得分:0)

为每个输入提供ng-model:

<input type="text" class="pure-input-1" id="input-2" ng-model="user.name" name="input-2">

现在,您可以使用以下命令来控制控制器中的输入值:

$scope.user.name 

详细了解此主题:https://docs.angularjs.org/guide/forms