[英]How to loop through array with arrays/objects and assign properties new values in Angular?
我有一個數組數組和對象。 我有一個要在其中為屬性分配值的函數(例如,$ scope.companies [0]的'call':''。users變為用戶在復選框中選中的任何值)。 我研究了它,但我只是不知道該怎么做,到目前為止我所做的一切都是錯誤的。 非常感謝!!
<form action="" ng-click="change(key)">
<input ng-model="key.call"type="checkbox"">Call
<br>
<input ng-model="key.person"type="checkbox" >Person
<br>
<input type="checkbox"ng-model="key.dial">Dial
<br>
<input type="checkbox" ng-model="key.voice">Voice
</form>
app.controller('appCtrl', function($scope) {
$scope.companies = [{
name: 'The Best Company Denim',
users: [{
firstName: 'Alex',
lastName: 'D',
number: 1234,
call: '',
person: '',
dial: '',
voice: ''
}, {
firstName: 'Sarah',
lastName: 't',
number: 14,
call: '',
person: '',
dial: '',
voice: ''
}, {
firstName: 'J',
lastName: 'd',
number: 07,
call: '',
person: '',
dial: '',
voice: ''
}]
}, {
name: 'The Best Company Elegant',
users: [{
firstName: 'Alx',
lastName: 'B',
number: 1234,
call: '',
person: '',
dial: '',
voice: ''
}, {
firstName: 'Seth',
lastName: 'w',
number: 12,
call: '',
person: '',
dial: '',
voice: ''
}, {
firstName: 'J.S',
lastName: 'B',
number: 7.
call: '',
person: '',
dial: '',
voice: ''
}]
}, {
name: 'The Best Company by Julia',
users: [{
firstName: 'Aleddddx',
lastName: 'l',
number: 1234,
call: '',
person: '',
dial: '',
voice: ''
}, {
firstName: 'Maggy',
lastName: 'n',
number: 1,
call: '',
person: '',
dial: '',
voice: ''
}, {
firstName: 'Ja',
lastName: 'Key',
number: 123,
call: '',
person: '',
dial: '',
voice: ''
}]
}]
$scope.change = function(key) {
for (var i = 0; i < $scope.companies[0].users; i++) {
$scope.companies[0].users[i].call: key)
}
}
});
根據上面的代碼, $scope.companies[0].users
是一個數組,具有一個length
屬性,該屬性可以為您提供其中的項目數。 然后您的代碼將是:
$scope.change = function(key) { for (var i = 0; i < $scope.companies[0].users.length; i++) { $scope.companies[0].users[i].call = key; }
如果您也在尋找公司之間的循環,那么請嵌套2個循環,一個循環公司,另一個循環用戶,其代碼與上述代碼相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.