$interval
的AngularJS文档提供了一个在控制器中使用$interval
来管理用户可以在视图中使用的计时器的示例。您可以在the angularJS documentation page by clicking n this link阅读官方示例的代码。
我试图将示例控制器中的代码移回服务中,以便代码可以更加模块化。但该应用程序未将服务连接到视图。我在a plnkr that you can play with by clicking on this link中重新创建了这个问题。
需要对上面的plnkr中的代码进行哪些具体更改,以便mytimer
服务作为导入服务的控制器的属性可用于视图?
总结一下,'index.html`是:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example109-production</title>
<script src="myTimer.js" type="text/javascript"></script>
<script src="exampleController.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
</head>
<body ng-app="intervalExample">
<div>
<div ng-controller="ExampleController">
<label>Date format: <input ng-model="mytimer.format"></label> <hr/>
Current time is: <span my-current-time="mytimer.format"></span>
<hr/>
Blood 1 : <font color='red'>{{mytimer.blood_1}}</font>
Blood 2 : <font color='red'>{{mytimer.blood_2}}</font>
<button type="button" data-ng-click="mytimer.fight()">Fight</button>
<button type="button" data-ng-click="mytimer.stopFight()">StopFight</button>
<button type="button" data-ng-click="mytimer.resetFight()">resetFight</button>
</div>
</div>
</body>
</html>
app.js
的代码是:
angular.module('intervalExample',['ExampleController'])
exampleController.js
的代码是:
angular
.module('intervalExample', ['mytimer'])
.controller('ExampleController', function($scope, mytimer) {
$scope.mytimer = mytimer;
});
myTimer.js
的代码是:
angular
.module('mytimer', [])
.service('mytimer', ['$rootScope', function($rootScope, $interval) {
var $this = this;
this.testvariable = "some value. ";
this.format = 'M/d/yy h:mm:ss a';
this.blood_1 = 100;
this.blood_2 = 120;
var stop;
this.fight = function() {
// Don't start a new fight if we are already fighting
if ( angular.isDefined(stop) ) return;
stop = $interval(function() {
if (this.blood_1 > 0 && this.blood_2 > 0) {
this.blood_1 = this.blood_1 - 3;
this.blood_2 = this.blood_2 - 4;
} else {
this.stopFight();
}
}, 100);
};
this.stopFight = function() {
if (angular.isDefined(stop)) {
$interval.cancel(stop);
stop = undefined;
}
};
this.resetFight = function() {
this.blood_1 = 100;
this.blood_2 = 120;
};
this.$on('$destroy', function() {
// Make sure that the interval is destroyed too
this.stopFight();
});
}])
// Register the 'myCurrentTime' directive factory method.
// We inject $interval and dateFilter service since the factory method is DI.
.directive('myCurrentTime', ['$interval', 'dateFilter',
function($interval, dateFilter) {
// return the directive link function. (compile function not needed)
return function(scope, element, attrs) {
var format, // date format
stopTime; // so that we can cancel the time updates
// used to update the UI
function updateTime() {
element.text(dateFilter(new Date(), format));
}
// watch the expression, and update the UI on change.
scope.$watch(attrs.myCurrentTime, function(value) {
format = value;
updateTime();
});
stopTime = $interval(updateTime, 1000);
// listen on DOM destroy (removal) event, and cancel the next UI update
// to prevent updating time after the DOM element was removed.
element.on('$destroy', function() {
$interval.cancel(stopTime);
});
}
}]);;
您可以用来诊断和识别问题解决方案的所有the above code is assembled in "working" form in this plnkr。 那么需要对上面的代码进行哪些具体更改才能允许用户通过视图与服务进行交互?
答案 0 :(得分:2)
首先,您没有向mytimer
服务注入$ interval并尝试使用它。
其次,您在mytimer
服务中遇到了范围问题:
stop = $interval(function() {
if (this.blood_1 > 0 && this.blood_2 > 0) {
this.blood_1 = $this.blood_1 - 3;
this.blood_2 = $this.blood_2 - 4;
} else {
this.stopFight();
}
}, 100);
在声明要创建新范围的函数时,意味着this
指向新范围。您可以使用bind
或使用您在第5行中声明的$this
变量。(在ES2015中,您只需使用箭头功能)。
你也在app.js
和mytimer.js
中声明了模块exampleController两次。
看看这个有效的Plunker:
http://plnkr.co/edit/34rlsjzH5KWaobiungYI?p=preview