【发布时间】:2015-10-18 14:28:31
【问题描述】:
如何将 ng-model 绑定到 ng-repeat 中的元素? 事实上,我想将数组中的对象绑定到元素,并且当输入元素类型时,使用新的 ng-model 创建新的输入元素。而在此示例中,所有 ng-model 都是相同的。
var myApp = angular.module('app', []);
myApp.controller("MyCtrl",function($scope){
$scope.items = [];
$scope.item = {
phone:""
};
$scope.items.push($scope.item);
$scope.addItem = function(index){
if($scope.items[index+1] == null)
$scope.items.push($scope.item);
console.log($scope.items);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
<div ng-repeat="line in items track by $index">
<!-- typing here should auto update it's preview above -->
<input ng-model="line.phone" ng-keydown="addItem($index)"/>
<!-- many other fields here that will also affect the preview -->
</div>
</div>
【问题讨论】:
-
是你想要的吗:
$scope.items.push(angular.copy($scope.item));? -
你到底想做什么?
-
@Ayush 我想动态创建表单。当输入输入元素时添加另一个输入元素。
标签: javascript angularjs