AngularJS:这两行有什么区别

AngularJS : what's the difference between these two lines?

本文关键字:两行 什么 区别 AngularJS      更新时间:2023-09-26
$scope.init = function() {
  return $scope.items = basketService.items;
};
ng-repeat = "item in items"

并使用$scope.items + 通过广播刷新$scope.items。

$scope.getItems = function() {
  return basketService.items;
};
ng-repeat = "item in getItems()"

将 basketService.items 复制到 $scope.items 是必须完成的,还是与 getItems(( 相同(速度、内存...(?

我不相信它们之间有区别,这实际上只是风格问题。 看看我创造的这个(非常粗糙和做作的(小提琴: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>

请注意,这两种变体都有效,并且都将为您提供双向绑定等。