私は自分のサービスで設定した変数を監視しようとしていますが、それが変更されたときにはコントローラの値は変更されますが、ウォッチイベントは発生しません。
これは私のコードです:
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