gpt4 book ai didi

javascript - 如何将此间隔服务连接到 View ?

转载 作者:行者123 更新时间:2023-11-29 21:33:14 24 4
gpt4 key购买 nike

$interval 的 AngularJS 文档给出了在 Controller 中使用 $interval 来管理用户可以在 View 中使用的计时器的示例。您可以在 the angularJS documentation page by clicking n this link 阅读官方示例的代码。 .

我已尝试将示例 Controller 中的代码移回到服务中,以便代码可以更加模块化。但是应用程序没有将服务连接到 View 。我在 a plnkr that you can play with by clicking on this link 中重新创建了问题.

需要对上面的 plnkr 中的代码进行哪些具体更改,以便 mytimer 服务可以作为导入服务的 Controller 的属性提供给 View ?

总结一下,“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您可以使用它来诊断和确定问题的解决方案。 那么需要对上面的代码进行哪些具体更改才能让用户通过 View 与服务进行交互?

最佳答案

首先,您没有将 $interval 注入(inject) mytimer 服务并尝试使用它。

其次,您在 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.jsmytimer.js 中声明了模块 exampleController 两次。

看看这个工作中的 Plunker:
http://plnkr.co/edit/34rlsjzH5KWaobiungYI?p=preview

关于javascript - 如何将此间隔服务连接到 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35588344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com