2015-07-28 17 views
0

如何將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>

+0

的陣列關鍵是你想要的東西:'$ scope.items.push(angular.copy($ scope.item));'? –

+0

你想要做什麼? – Ayush

+0

@ Ayush我想要動態地創建表單。當在input元素中輸入時添加另一個輸入元素。 –

回答

1

如果傳遞像$scope.items.push($scope.item);然後$scope.item是參考同一個對象,它會推到陣列多次,因爲對象是被引用類型,如果推primitive數據類型它將行爲不同,

定義item本地的功能,

$scope.addItem = function(index){ 
     if($scope.items[index+1] == null) { 
      var item = { 
       phone:"" 
      }; 
     } 
     $scope.items.push(item); 
     console.log($scope.items); 
    } 

var myApp = angular.module('app', []); 
 
myApp.controller("MyCtrl",function($scope){ 
 
    $scope.items = []; 
 
    var item = { 
 
       phone:"" 
 
    }; 
 
    $scope.items.push(item); 
 

 
    $scope.addItem = function(index){ 
 
     if($scope.items[index+1] == null) { 
 
      var item = { 
 
       phone:"" 
 
      }; 
 
      $scope.items.push(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>

+0

謝謝。它的工作正常,但在控制檯出現此錯誤:錯誤:[ngModel:nonassign] http://errors.angularjs.org/1.3.14/ngModel/nonassign? –

+0

umm不知道什麼確切的錯誤,在這個演示中沒有控制檯錯誤:( –

+0

@ K.Toress。好的。這是正確的,沒有錯誤的控制檯。非常感謝。你的答案和simoco答案是正確的,我不'不知道哪個接受:( –

2

我想你想這一個。請注意,我用

$scope.items.push(angular.copy($scope.item)) 

做出複製對象的。沒有這個,你總是把參考相同的對象放在數組中,所以改變其中的一個也會導致其他的被改變。

var myApp = angular.module('app', []); 
 
myApp.controller("MyCtrl",function($scope){ 
 
    $scope.items = []; 
 
    $scope.item = { 
 
     phone:"" 
 
    }; 
 
    $scope.items.push(angular.copy($scope.item)); 
 

 
    $scope.addItem = function(index){ 
 
     if($scope.items[index+1] == null) 
 
      $scope.items.push(angular.copy($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>

+0

如何刪除值時添加新元素? –

+0

@hadi,我認爲這是在另一個[_tread _](http: //stackoverflow.com/questions/15453979/how-do-i-delete-an-item-or-object-from-an-array-using-ng-click) - 它有完美的解釋,只需添加'ng-click =「remove(line)」''並在你的控制器中添加'remove'行爲 –

+0

當輸入第二個輸入元素時,它添加了帶有第一個輸入元素值的新輸入元素當我不需要這個時 –

1

您也可以使用密鑰作爲模型

<div ng-repeat="(key, line) in items track by $index"> 
    <input ng-model="line.phone[key]" ng-keydown="addItem($index)"/> 
</div> 
+0

你忘了提到它實際上做了什麼:'

{{items|json}}
' - 我不認爲這是OP想要的 –

+0

OP應該將手機聲明爲數組。像'$範圍。item = { phone:[] };'那麼你的上面的代碼將完美地工作:-) – Vineet