我刚刚开始使用 Angular 并且可以使用手动重新:绑定到范围。
我正在尝试加载填充属性的数据。然后我想根据这个属性更新其他属性,有什么方法可以轻松触发吗?
目前我只是在做:
- 加载 JSON 数据
- 放
mythings
- 触发计算
mythings_processed
我觉得我应该能够用(2)触发(3 ) ?
例子
Javascript
app.controller('MyCtrl', function($scope, $http) {
$scope.loadData = function () {
$http.get('/api/orgs/gocardless/issues.json').success(function(data) {
$scope.mythings = data.things;
$scope.generateCalculatedItems() // Surely this isn't necessary?
});
};
$scope.generateCalculatedItems = function () {
mythings_processed = {}; // Generated using mythings
$scope.mythings_processed = mythings_processed;
};
};
HTML
<div ng-controller="IssuesCtrl" ng-init="loadData()">
<ul>
<li ng-repeat="thing in things">{{thing.id}}</li>
</ul>
<ul>
<li ng-repeat="processed_thing in mythings_processed">{{processed_thing.id}}</li>
</ul>
</div>
我确定我在这里对 Angular 做了一些可怕的事情,所以我道歉!
我确实觉得我应该能够做到:
$scope.mythingsProcessed = function () {
mythings_processed = {}; // Generated using mythings
return mythings_processed;
};
<ul>
<li ng-repeat="processed_thing in mythingsProcessed()">{{processed_thing.id}}</li>
</ul>
但是当我这样做时,它给了我错误:10 $digest() iterations reached. Aborting!
有任何想法吗?
更新:示例数据/用例
我有一个通过 ajax 获取的项目列表:
{
items: [
{name: 'pete', value: 1},
{name: 'john', value: 2},
{name: 'pete', value: 3},
{name: 'steve', value: 2},
{name: 'john', value: 1}
]
}
我想显示所有这些,但我也想将它们处理(总和)为:
{
processed_items: [
{ name: 'pete', value: 4 }
{ name: 'john', value: 3 }
{ name: 'steve', value: 2 }
}
然后将它们也显示在列表中。