내 서비스에서 설정 한 변수를보고 싶습니다. 변경 될 때 - 컨트롤러의 값을 변경하지만 감시 이벤트가 실행되지 않습니다.
이 내 코드입니다 :
var module = angular.module("Test", []);
module.service("testService", TestService);
module.controller("testController", TestController);
function TestService()
{
var self = this;
self.Message = "1";
}
TestController.$inject =["$scope", "testService"];
function TestController($scope, testService)
{
var self = this;
self.Message = testService.Message;
$scope.Message = self.Message;
$scope.$watch("Message", onMessageChange, true)
self.click = function()
{
testService.Message = "2";
}
function onMessageChange(oldVal, newVal)
{
self.Message = newVal;
}
}
이는 HTML입니다 :
<!DOCTYPE html>
<html >
<head>
<title>Test</title>
<script src=".\angular.js"></script>
<script src=".\test.js"></script>
</head>
<body ng-app="Test">
<div ng-controller="testController as ctrl">
<button ng-click="ctrl.click()"> Click </button>
<span> Message: {{ctrl.Message}}</span>
</div>
</body>
</html>
가 왜 작동하지? 내가 도대체 뭘 잘못하고있는 겁니까?
변화 그것을 메시지 : {{메시지}} – MMK