在這里,我試圖獲取一個 0 到 3 之間的亂數,并根據它從陣列 buttonColours 中選擇一個元素,然后將相同的元素添加到陣列 gamePattern 中,生成 randomChosencolour 字串(一種隨機顏色)并在每次單擊時顯示按鈕,但 gamePattern 陣列似乎不起作用,
模板 HTML:
<h1>{{randomChosenColour}}</h1>
<h1>{{gamePattern}}</h1>
<button class="btn-primary" (click)="nextSequence()">click here</button>
----------
TS file:
max_limit=3;
min_limit=0;
randomNumber=0;
buttonColours = ["red", "blue", "green", "yellow"];
randomChosenColour="";
gamePattern =[] as any;
nextSequence()
{
this.randomNumber = this.min_limit Math.floor(Math.random()*(this.max_limit-this.min_limit 1));
this.randomChosenColour = this.buttonColours[this.randomNumber];
this.gamePattern.push(this.randomChosenColour);
}
我試過這種方法也無濟于事。
this.gamePattern.push(Object.assign({}, this.randomChosenColour));
uj5u.com熱心網友回復:
請添加*ngFor以顯示陣列中的所有顏色。
<h1>Random Color:{{ randomChosenColour }}</h1>
<h1 *ngFor="let color of gamePattern">{{ color }}</h1>
<button class="btn-primary" (click)="nextSequence()">click here</button>
這是作業演示鏈接:https : //stackblitz.com/edit/angular-ivy-eagqga?file= src/app/ app.component.html
uj5u.com熱心網友回復:
使用*ngFor迭代并顯示陣列。
<h1>{{randomChosenColour}}</h1>
<h1 *ngFor="let colour of gamePattern">{{colour}}</h1>
<button class="btn-primary" (click)="nextSequence()">click here</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/398060.html
