plunker preview
我有主要數組與關於用戶的信息。
每個項目(用戶)具有 '數據' 數組:
{
name: 'Linda',
data: [
{
value: 3
},
{
value: 6
}
]
}
我需要打印的主陣列是這樣的:
- 琳達 - 3
- 琳達 - 6
已更新:我可以在輸入中更改名稱。見版本2.
所以,我用ng-repeat函數返回新的對象。而我得到知錯誤:[$ rootScope:infdig]
爲了避免這個錯誤我使用google groups辦法:「變化」過濾器和「連接器」的工廠。
(我想用$$ hashKey的方法,但我不明白怎麼做)
爲了得到這個錯誤,打開我的代碼並刪除HTML中的'更改'過濾器。
問題:也許你知道打印數組更容易嗎?謝謝。
如果我打印前打印?
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.users = [
{
name: 'Linda',
data: [
{
value: 3
},
{
value: 6
}
]
},
{
name: 'John',
data: [
{
value: 2
},
{
value: 11
}
]
}
];
$scope.getUsersData = function(){
var output = [];
$scope.users.forEach(function(user){
if ('data' in user) {
user.data.forEach(function(userData){
output.push({name: user.name, value: userData.value});
});
}
});
return output;
};
});
// From https://groups.google.com/d/msg/angular/IEIQok-YkpU/iDw32-YRr3QJ
app.filter('change', function(linker) {
return function(items) {
var collection = items.map(function (item) {
return item;
});
return linker(collection, 'some-key');
};
});
app.factory('linker', function() {
var links = {};
return function (arr, key) {
var link = links[key] || [];
arr.forEach(function (newItem, index) {
var oldItem = link[index];
if (!angular.equals(oldItem, newItem))
link[index] = newItem;
});
link.length = arr.length;
return links[key] = link;
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<body ng-app="plunker" ng-controller="MainCtrl as main">
<ul>
<li ng-repeat="user in users = getUsersData() | change track by $index">{{user.name}} - {{user.value}}</li>
</ul>
<hr>
<div ng-repeat="user in users">
<input ng-model="user.name"/>
</div>
</body>
你不能在ngRepeat使用功能是這樣的。 – MMK