1

데이터 모델이 변경되면 바로 각도 새로 고침을 시도하고 있습니다. 데이터 저장소 객체는 다소 복잡한 모델이지만 결국에는 화면에 렌더링해야하는 데이터에 각도를 제공해야합니다. 내가 알 수없는 것은 사물이 다르다는 것을 각도에 알리는 방법입니다.변경 각도 알림

$ scope를 사용해야하는 방법을 완전히 이해하지 못했지만 내게는 제대로 작동하지 않습니다. 이것이 사소한 문제라면 미안합니다.

<html ng-app="testApp"> 
    <head> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> 
     <script> 
      function Store() { 
       this.data = [{ msg: "a" }, { msg: "b" }]; 

       this.performWeirdModifications = function() { 
        data.push({ msg: "c" }); 
        data.push({ msg: "d" }); 
       } 
      } 

      var store = new Store(); 

      angular.module('testApp', []).controller('DataController', function() { 
       this.data = store.data; 
      });    
     </script> 
    </head> 
    <body ng-controller="DataController as dataCtrl"> 
     <button onclick="store.performWeirdModifications()">Simulate something</button> 
     <div ng-repeat="data in dataCtrl.data">{{data.msg}}</div> 
    </body> 
</html> 
+0

. $이 문제를 해결하기 위해 적용, 난이 점점 아니에요 무엇을, 왜 당신이 더 많은 각도 방법입니다 서비스 ... – harishr

+0

문제는 그대로 스토어를 사용하지 말아 여기 상점은 많은 것들을하는 레거시 코드 더미를 실제로 판단합니다. 그렇지 않으면 처음부터 모퉁이 모양으로 만들 것입니다. –

답변

2

$scope에 추가하십시오.

angular.module('testApp', []).controller('DataController', function ($scope) { 
     $scope.data = store.data; 
    }); 

다음은 서비스를 통해 수행했을 것입니다. 당신이 $ 범위를 사용할 수 있지만

<html ng-app="testApp"> 
    <head> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> 
     <script> 

      angular.module('testApp', []) 
      .service('Store', function() { 
       this.data = [{ msg: "a" }, { msg: "b" }]; 

       this.performWeirdModifications = function() { 
        this.data.push({ msg: "c" }); 
        this.data.push({ msg: "d" }); 
       }; 
       return this; 
      }) 
      .controller('DataController', function ($scope, Store) { 
       $scope.data = Store.data; 
       $scope.getData = function() { 
        Store.performWeirdModifications(); 
        $scope.data = Store.data; 
       }; 
      });    
     </script> 
    </head> 
    <body ng-controller="DataController as dataCtrl"> 
     <button ng-click="getData()">Simulate something</button> 
     <div ng-repeat="d in data">{{d.msg}}</div> 
    </body> 
</html> 
+0

아, 내가 찾고있는 것. 정말 고맙습니다! –