2016-07-28 5 views
0

としてコントローラを使用して、コントローラに変更されたとき、私は、コントローラ内の以下の機能を持っているビューを更新しない:はNGリピート - 配列が構文

app.controller("myCtrl", function(myServ, $scope){ 
    var ctrlScope = this; 
    ctrlScope.myData = []; 

    function getData(){ 
    myServ.getData().then(function(data){ 
     $scope.$evalAsync(function(){ 
     ctrlScope.myData = data; 
     }); 
    }); 
    } 

    getData(); // myData is initialised properly 

    ctrlScope.update = function(){ 
    // Data has already changed in backend 
    getData(); //myData inside controller is updated but ng-repeat is not able to show on the view the updated array 
    }; 
}); 

HTML:

<div data-ng-controller="myCtrl as mc"> 
    <span data-ng-repeat="d in mc.myData">{{d.name}}</span> 
    <button data-ng-click="mc.update()"> Update </button> 
</div> 

getData()ng-repeatは正常に初期化されますが、コントローラがデータを更新した場合でもビューを更新することはできません。

+0

を追加しますか?それはjQueryのようなサードパーティーですか? – AranS

+0

これは、他のアクションによって更新されていますが、ここでは表示されず、質問が明確になります。 –

+0

[[d.name]] は中括弧で置き換えます – gyc

答えて

0

アレイの参照を変更しています。そのため、UIでデータが更新されないのです。あなたは以下のようにデータを更新することができます。

function getData(){ 
    myServe.getData().then(function(data){ 
     ctrlScope.myData.length = 0; 
     ctrlScope.myData.push.apply(ctrlScope.myData, data); 
    }); 
} 
+0

うん、私は前にこれを試したが運がない。 –

0

どのようにデータを更新しない$scope().apply()

ctrlScope.update = function(){ 
     // Data has already changed in backend 
     getData(); //myData inside controller is updated but ng-repeat is not able to show on the view the updated array 

     $scope().apply(); 
     }; 
+0

$ scope。$ evalAsyncをコードに追加するのを忘れてしまったので、基本的に現在のダイジェストサイクルで更新をキューに入れます。 $ scope。$ applyは、角度の範囲から外れていないので、必要ではありません。ここでのシナリオではない角度の範囲外の更新を行うときに必要です。 –