如何將ng-model綁定到ng-repeat內的元素? 實際上,我想將數組中的對象綁定到元素,並在輸入元素中鍵入時使用新的ng模型創建新的輸入元素。而在這個例子中所有的ng模型都是一樣的。ng-model與ng-repeat中的元素綁定Angularjs
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
@ Ayush我想要動態地創建表單。當在input元素中輸入時添加另一個輸入元素。 –