1
$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() (speed, memory...) と同じですか?

4

1 に答える 1

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>

両方のバリアントが機能し、どちらも双方向バインディングなどを提供することに注意してください。

于 2013-07-09T23:45:45.040 に答える