2017-07-13 1 views
0

aa $ウォッチコールのスコープ変数を更新したいときにこれを更新すると、更新されたng-repeatアップデート。

ここにはすべてのコードがあります。あなたも$ウォッチを必要としない。このケースでは https://plnkr.co/edit/LB8CCvtEZG76D8plMgu6?p=preview

link: function($scope, $el,$attrs) { 
     $scope.$watch(function(scope){ 
      return PlayerlistS.getList(); 
     }, 
     function (newVal, oldVal) { 
      console.log("CHANGED"); 

      /*I want this to contain the new array and then the ng-repeat must update with the new values.*/ 
      $scope.players = newVal; 
     }, true); 
    } 

答えて

1

。ディレクティブで工場名のスペルを間違えました。サービス内の配列&をコントローラで呼び出すと、自動的に更新されます。ので、あなたのディレクティブのコードは、次にコントローラにあなたは$scope.players = PlayerListS.getList();を持つことができますし、自分の工場内でその配列を持っている

app.directive("player", ['PlayerListS', function (PlayerListS) { 
    return { 
     restrict: 'E', 
     scope: { 
      person:'@person', 
      add:'&add' 
     }, 
     replace: false, 
     templateUrl: "player.html", 
     controller: function($scope, $element, $compile) { 
       $scope.add = function(name) { 
        PlayerListS.addToList(name); 
        var get = PlayerListS.getList(); 
        $scope.newName = ""; 
        console.log(get); 
       } 
     } 
    }; 
}]); 

することができます。ここであなたの要件のplunkerバージョンを働いている:https://plnkr.co/edit/BJofKoeIplaNjoy3k5TW?p=preview

関連する問題