Angular 角度-单击后更改变量的角度
假设我有一个ngFor循环:Angular 角度-单击后更改变量的角度,angular,loops,variables,ngfor,Angular,Loops,Variables,Ngfor,假设我有一个ngFor循环: <ng-container*ngFor="let rate of [1,2,3,4,5]"> <div (click)="change()">{{myVariable}}</div> </ng-container> 如果我单击a,则所有内容都将更改为b。如何仅对单击的项目和以前的项目应用此更改?因此,例如,从aaaaaaa到bbbaa?只需传递所选评级的值即可 类似这样的方法可能会奏效: <ng-con
<ng-container*ngFor="let rate of [1,2,3,4,5]">
<div (click)="change()">{{myVariable}}</div>
</ng-container>
如果我单击
a
,则所有内容都将更改为b
。如何仅对单击的项目和以前的项目应用此更改?因此,例如,从aaaaaaa
到bbbaa
?只需传递所选评级的值即可
类似这样的方法可能会奏效:
<ng-container*ngFor="let rate of [1,2,3,4,5]">
<div (click)="change(myVariable)">{{myVariable}}</div>
</ng-container>
尝试以下方法:
<ng-container*ngFor="let value of values; index as i">
<div (click)="change(i)">{{value}}</div>
</ng-container>
export class StarRatingComponent {
values = this.generateValues('a',5);
private generateValues(val: string, times: number): string[]{
const result: string[] = [];
while(--times >= 0){
result.push(val);
}
return result;
}
change(index: number) {
while(index >= 0){
this.values[index] = 'b';
index--;
}
}
}
{{value}}
导出类StarRatingComponent{
values=此.generateValues('a',5);
私有生成值(val:string,times:number):string[]{
常量结果:字符串[]=[];
而(--次>=0){
结果:推送(val);
}
返回结果;
}
更改(索引:编号){
而(索引>=0){
这个。值[索引]=“b”;
索引--;
}
}
}
遗憾的是,它不起作用了。+我知道如何接收点击项目的数量,所以如果有人点击第三个a,我会得到“3”,但不知道如何将此“a”更改为“b”,但感谢您的帮助和时间。在这种情况下,我们缺少信息。你到底想改变什么?组件上只有一个“a”,当然所有组件都已更改?
export class RatingCmp {
rating: string = '1';
change(rating) {
this.rating = rating;
}
}
<ng-container*ngFor="let value of values; index as i">
<div (click)="change(i)">{{value}}</div>
</ng-container>
export class StarRatingComponent {
values = this.generateValues('a',5);
private generateValues(val: string, times: number): string[]{
const result: string[] = [];
while(--times >= 0){
result.push(val);
}
return result;
}
change(index: number) {
while(index >= 0){
this.values[index] = 'b';
index--;
}
}
}