2017-10-26 6 views
0

내 앱에서 다음 코드를 실행하고 ng-init이 $ watch를 트리거하지 않는 경우가 있습니다. 어떤 아이디어?

HTML

<div ng-controller="MyCtrl"> 
    <p ng-init="stages = 'coco'">{{x}}</p> 
</div> 

컨트롤러

var myApp = angular.module('myApp',[]); 

    function MyCtrl($scope) { 

     $scope.$watch("stages", function() { 
      alert($scope.stages); 
     }); 
    } 

답변

1

없음은, 당신의 코드에 따라 어디서든 ng-app을 추가하지 않은 않습니다.

+0

죄송합니다

var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl',function($scope){ $scope.$watch("stages", function() { alert($scope.stages); }); });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="MyCtrl"> <p ng-init="stages = 'coco'">{{x}}</p> </div>
DEMO

는 응용 프로그램의 코드의 일부입니다. 나는 앱 로그에서 $ watch가 실행되지 않는 것을 보았습니다, 가능합니까? – CocoTown

+0

아니요, 변수가 변경 될 때마다 항상 트리거됩니다. – Sajeetharan