我正在尝试在 ng-click 事件上生成随机数。目前它正在生成一个随机数,但仅生成一次,然后依赖浏览器刷新来生成另一个数字。
我相信这可能与我声明变量样本的方式有关。
angular.module('FOO', [])
.component('foo', {
controller: fooController,
controllerAs: 'foo',
template: template()
});
function template() {
return [
'<button ng-repeat="move in foo.moves" ng-click="foo.gameplay(move)" >',
'{{ move }}',
'</button>',
].join('')
}
function fooController() {
var self = this;
self.gameplay = gameplay;
self.moves = ['var1', 'var2', 'var3']
var sample = Math.floor(Math.random() * (self.moves.length));
function gameplay(clicker) {
console.log(sample);
}
}
最佳答案
试试这个
function fooController() {
var self = this;
self.gameplay = gameplay;
self.moves = ['var1', 'var2', 'var3']
var sample = Math.floor(Math.random() * (self.moves.length));
function gameplay(clicker) {
sample = Math.floor(Math.random() * (self.moves.length));
console.log(sample);
}
}
关于javascript - 使用 Angular 1.5 中的组件生成随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39288722/