我想在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);
}
}
答案 0 :(得分:0)
试试这个
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);
}
}
答案 1 :(得分:-1)
为什么不使用
function gameplay(clicker) {
var sample = Math.floor(Math.random() * (self.moves.length));
console.log(sample);
}