【发布时间】:2019-11-16 00:58:46
【问题描述】:
我有一个与以下示例类似的应用程序,但我无法弄清楚源数据未更新的原因。更多信息在示例 cmets 中。我确定这是我忽略的一些小问题。
控制器
$scope.items = [
{ id: 1, color: 'red', title: 'car' },
{ id: 2, color: 'blue', title: 'sky' },
{ id: 3, color: 'transparent', title: 'nothing' }
]
$scope.favoriteIds = [1, 2, 3]
$scope.getItem = function(id) { /* returns an item with given id */ }
最后,有两种方法可以修改$scope.items,但只有第一种有效,因为新项目获得了not-already-known id。 p>
$scope.changeData1 = function() {
$scope.items = [{ id: 666, color: 'ugly', title: 'face' }]
$scope.favoriteIds = [666]
}
$scope.changeData2 = function() {
$scope.items = [{ id: 1, color: 'ugly', title: 'face' }]
$scope.favoriteIds = [1]
}
查看
<h1>Favourite items</h1>
<ul ng-repeat="id in favoriteIds" data-ng-init="item = getItem(id)">
<li>I like my {{ item.color }} {{ item.title }}</li>
</ul>
<button ng-click="changeData1()">Modify data</button>
<!-- prints: I like my ugly face -->
<button ng-click="changeData2()">Modify nothing</button>
<!-- prints: I like my red car -->
问题是,我需要使用第二种方式来修改数据。
【问题讨论】:
标签: javascript angularjs angularjs-ng-repeat