9

ng-repeatディレクティブを含むページがあります。ページが最初にロードされたときに機能しますが、の内容を更新するng-repeatために使用できるようにしたいです。次のコードを試しましたが、うまくいきません。助言がありますか?ng-clickng-repeat

<div ng-click="loadItems('1')">Load 1st set of items</div>
<div ng-click="loadItems('2')">Load 2nd set of items</div>
...

<table>
    <tr ng-repeat="item in items">>
        // stuff
    </tr>
</table>

項目コントロール:

$scope.loadItems = function (setID) {
    $http({
        url: 'get-items/'+setID,
        method: "POST"
    })
    .success(function (data, status, headers, config) {
        $scope.items = data;
    })
    .error(function (data, status, headers, config) {
        $scope.status = status;
    });
};

への呼び出しにより、サーバーから取得した新しいデータでディレクティブがリロードされるloadItems()ことを期待していました。ng-repeat

4

1 に答える 1

10

コールバックにブロードキャストを追加し、コントローラーでそれをサブスクライブします。

これは本当にサービスにあるはずです

itemsService.loadItems = function (setID) {
    $http({
        url: 'get-items/'+setID,
        method: "POST"
    })
    .success(function (data, status, headers, config) {
        $scope.items = data;
        $rootScope.$broadcast('updateItems', data);
    })
    .error(function (data, status, headers, config) {
        $scope.status = status;
    });
}; 

コントローラーで:

$scope.$on("updateItems",function(d){
  $scope.items = d;
});

だからいつでもng-click="update(id)"

$scope.update = function(id){
    itemsService.loadItems(id);
}

購読itemsしているため、自動的に更新されます。

于 2013-07-23T17:02:46.597 に答える