$scope.init = function() {
return $scope.items = basketService.items;
};
ng-repeat = "item in items"
使用$ scope.items +刷新$ scope.items广播。
OR
$scope.getItems = function() {
return basketService.items;
};
ng-repeat = "item in getItems()"
将basketService.items复制到$ scope.items是必须要做的还是和getItems()(速度,内存......)相同?
答案 0 :(得分:0)
我不相信他们之间存在差异,这只是风格问题。看看我创建的这个(非常粗糙和做作的)小提琴:http://jsfiddle.net/digitalzebra/92gA6/
这是一个非常混乱的控制器:
angular.module("foobar", []).controller("lol", function($scope) {
$scope.items = ["loe", "le", "effe"];
var blah = ["leoele", "elelkej", "elfkjflkje"];
$scope.doStuff = function() {
$scope.items.push("eee", "eeelkjf");
};
$scope.getItems = function() {
return blah;
}
$scope.doOtherStuff = function() {
blah.push("elejlee'e");
};
});
这是我的HTML:
<div ng-app="foobar">
<div ng-controller="lol">
<b>First list:</b>
<div ng-repeat="item in items">
{{item}}
</div>
<b>Second list:</b>
<div ng-repeat="item in getItems()">
{{item}}
</div>
<button ng-click="doStuff()">Click me</button>
<button ng-click="doOtherStuff()">Do Other stuff</button>
</div>
</div>
请注意,这两种变体都有效,并且两者都会为您提供双向绑定等。