为什么在 1、2、3 种情况下更改服务中的消息不会影响显示的消息?
var app = angular.module('app', []);
app.factory('Message', function() {
return {message: "why this message doesn't changed"};
});
app.controller('Changer', function($scope, Message) {
Message.message = "first" // (1)
$scope.changeItems = function() {
Message.message = "second" // (2)
}
});
app.controller('Listener', function($scope, Message) {
$scope.message = Message.message
Message.message = "third" // (3)
});
而我的观点:
<div ng-controller="Listener">
{{ message }}
</div>
<div ng-controller="Changer">
<button ng-click="changeItems()">change message</button>
</div>
我还在 plunkr http://plnkr.co/edit/BUPS6U0S7ktDEkH9dZTZ?p=preview上创建了示例