我有一个this simple jsfiddle,它展示了我认为可以被注入到任何控制器中并进行监控的动态服务值。以下是Angular代码:
var app = angular.module("application", []);
app.service("valueSetter", function(){
that = this;
this.serviceValue = {count: 1};
this.updatevalue = function(){
that.serviceValue.count = that.serviceValue.count + 1;
console.log(that.serviceValue);
};
});
app.controller("appcontroller", function($scope, valueSetter){
$scope.scopeValue = valueSetter.serviceValue.count;
$scope.update = function(){
valueSetter.updatevalue();
};
});
而且HTML很简单:
<div ng-app="application" ng-controller="appcontroller">
Scope value is: {{scopeValue}}<br /><br />
<input type="button" value="update" ng-click="update()" />
</div>
如果您观察控制台,您会看到服务值增加了,但增加的值没有反映在控制器作用域中。我是否误解了它应该如何工作?