简体   繁体   English

如何在角度变量中使用 =

[英]How to use the = in an angular variable

I need to do something like that:我需要做这样的事情:

<app-popover #popName texto="{{'texto'='errorName1' | translate}}" ></app-popover>
<app-popover #popName texto="{{'texto'='errorName2' | translate}}" ></app-popover>
<app-popover #popName texto="{{'texto'='errorName3' | translate}}" ></app-popover>

But it returns an error但它返回一个错误

I have to send this to another html where the text will be printed, but since the code is reused and it is a translator what I use I have to be able to use different variable names:我必须将它发送到另一个将打印文本的 html,但是由于代码被重用并且它是我使用的翻译器,因此我必须能够使用不同的变量名称:

<ng-template #popContent let-greeting="greeting">{{"texto"}}!</ng-template>

Why don't you catch it in the TS/JS and use a switchcase?为什么不在 TS/JS 中捕获它并使用开关箱?

switch (this.texto) {
  case this.error1:
    this.texto = this.error1;
    break;
  case this.error2:
    this.texto = this.error2;
    break;
}

Alternate option is to use an if/else statement.替代选项是使用 if/else 语句。

Sorry, I think your question is a little bit unclear.抱歉,我觉得你的问题有点不清楚。 If I guess right what you want to do you should: 1. bind the property to the error like [textto]=error 2. accept the input via @input in your app-popover component 3. translate the text directly in the app-popover component如果我猜对了您想要做什么,您应该: 1. 将属性绑定到错误,例如 [textto]=error 2. 在 app-popover 组件中通过 @input 接受输入 3. 直接在 app 中翻译文本-弹出组件

I think this way is clearer and more easy to reuse since you can just use an array and loop if you get more than 3 errors without having to go back.我认为这种方式更清晰,更容易重用,因为如果出现超过 3 个错误,您可以只使用数组和循环,而不必返回。 I think this approach should also get rid of your error because I think the line texto="{{'texto'='errorName1' | translate}}" is too prone to errors.我认为这种方法也应该摆脱你的错误,因为我认为 texto="{{'texto'='errorName1' | translate}}" 这行太容易出错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM