[英]Angular 8 : How to send the value from *ngfor to TS file
I have a code我有一个代码
<div *ngFor="let a of ABC">
{{a.error}}
</div>
Now I want to send the value of a to my TS file.现在我想将 a 的值发送到我的 TS 文件。 I want to meet some expectation say 'a.error == true' then call some function and assign the value of a to some variable in ts file.
我想满足一些期望说'a.error == true'然后调用一些function并将a的值分配给ts文件中的某个变量。
I hope it is clear.我希望很清楚。 please ask if require more clarification
请询问是否需要更多说明
To communicate with the ts file as you say you just call a function which you declare如您所说,要与 ts 文件进行通信,您只需调用您声明的 function
// In your ts.file
someFunction(a: any) {
// some amazing stuff
return 'Hello World';
}
and in your template并在您的模板中
<div *ngFor="let a of ABC">
{{ someFunction(a) }}
</div>
You should probably loop the variable ABC
in the TS file before exposing it to the html template.在将变量 ABC 暴露给 html 模板之前,您可能应该在 TS 文件中循环变量
ABC
。 Be aware of the fact that.请注意这一事实。 if you call a function in the html, it will be evaluated many many times.
如果你在html中调用一个function,它会被多次评估。
eg , the following will execute the someFunction
many more times than you probably need.例如,以下将执行
someFunction
比您可能需要的次数更多。
//.html
<div *ngFor="let a of ABC">
{{someFunction(a)}}
</div>
I suggest creating the values you need to read in the html as a different var.我建议创建您需要在 html 中读取的值作为不同的 var。 for example.
例如。
//.ts
ERR: string[];
ngOnInit() {
this.ERR = ABC.map(a => someFunction(a));
}
Then in the template just bind to the error array然后在模板中绑定到错误数组
//.html
<div *ngFor="let a of ERR">
{{a}}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.