1
是否可以使用$ watch绑定serviceProvider的日期?AngularJS - 绑定到提供程序属性
我的问题是我可以将数据从提供者服务绑定到我的视图?
我尝试绑定我的视图中提供的$ get对象,因为提供程序在init阶段加载,没有控制器。
我尝试这种方式,但我没有更新。 http://plnkr.co/edit/t3PrE6lX1EDIMuqKyTW0?p=preview
<body ng-app="ServiceNotification">
<div style="border-style:dotted" ng-controller="TimerCtrl1">
TimerCtrl1<br/>
Last Updated: {{lastUpdated}}<br/>
Last Updated: {{calls}}<br/>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script type="text/javascript">
var app = angular.module("ServiceNotification", []);
function TimerCtrl1($scope, Timer) {
$scope.$watch(function() { return Timer.data; }, function (data){
console.log("In $watch - data:" + data);
$scope.lastUpdated = data.lastUpdated;
$scope.calls = data.calls;
}, true); // <-- don't forgt the true
};
app.provider("Timer", function() {
this.$get = function() {
var data = { lastUpdated: new Date(), calls: 0 };
var updateTimer = function() {
data.lastUpdated = new Date();
data.calls += 1;
console.log("updateTimer: " + data.lastUpdated);
window.setTimeout(updateTimer, 5000);
};
updateTimer();
return {
data: data
}
}
});
</script>
感谢托马斯
请在这里发表您的代码,如果该链接将到期,或将成为unaccessable,这个问题将有较少的意识。 –