我有以下打字稿。
应该将用户输入的报价随机化
quoteReady: boolean = false;
randomizQuote(array);
{
this.quoteReady = true;
this.quote = array;
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
2.这是我在html中放入的内容,我想做的是取随机报价,并在每个表格中放入2至3个字母
<section *ngIf="quoteReady">
<table class="table table-bordered table-hover table-responsive">
<thead>
<td>
<tr *ngFor="let quo of quote">{{ citation }}</tr> //i want to insert a
randome 2 to 3 //letters from the quote here
</td>
<td>
<tr *ngFor="let quo of quote">{{ array }}</tr>
</td>
<td>
<tr *ngFor="let quo of quote">{{ array }}</tr>
</td>
<td>
<tr *ngFor="let quo of quote">{{ array }}</tr>
</td>
<td>
<tr *ngFor="let quo of quote">{{ array }}</tr>
</td>
</thead>
</table>
</section>
因此,我不确定为什么没有给出输出。
根据这个答案
你可以在你的 ts
import { Component, VERSION, OnInit} from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
randomQuote = [];
numberOfLetters = 3
quote = "abcdefghijklmonopqrstuwxyz".split('');
quoteIndexes = {}
ngOnInit(){
while (this.randomQuote.length < this.numberOfLetters){
let index = -1;
do {
index = Math.floor(Math.random() * this.quote.length);
this.quoteIndexes[index] = true;
} while (!(index in this.quoteIndexes))
this.randomQuote.push(this.quote[index]);
}
}
}
然后
<td>
<tr *ngFor='let letter of randomQuote'>{{letter}}</tr>
</td>
在这里,您绝对需要字母的数量小于或等于引号长度,并确保避免空引号
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句