我的代码如下:
HTML
<section ng-controller="randController as rand">
Skriv inn minimumtall:<br>
<input ng-model="rand.minimum" type="number" placeholder="Minste tall"><br>
<div class="tiny"></div>
Skriv inn makstall:<br>
<input ng-model="rand.maksimum" type="number" placeholder="Største tall"><br><br>
<ul class="nav nav-pills" ng-controller="clickController as click">
<li ng-click="click.randClick()" ng-class="{'active' : click.randCheck(true)}" >
<a ng-click="rand.tilfeldig()">Randomize!</a>
</li>
</ul>
<div class="tiny"></div><br>
<pre>Tilfeldig tall = {{rand.tilfeldig()}}</pre>
</section>
JS
.controller('clickController', function ($timeout) {
this.randClicked = false;
this.randClick = function () {
this.randClicked = true;
$timeout(function() {
this.randClicked = false;
}, 1000, true);
};
this.randCheck = function (x) {
return this.randClicked === x;
};
})
我想做的是重置 <li>
的事件类- 大约 2 秒后的元素(点击 2 秒后取消点击)。我已经从论坛成员那里得到了提示,但它仍然不起作用。目前,我得到 this error ,我不明白。
最佳答案
您的代码中的this
指针有问题。试试这个:
.controller('clickController', function ($timeout) {
var that = this;
this.randClicked = false;
this.randClick = function () {
this.randClicked = true;
$timeout(function() {
that.randClicked = false;
}, 1000, true);
};
this.randCheck = function (x) {
return this.randClicked === x;
};
})
关于javascript - 如何在 AngularJS 中为函数计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33615525/