简体   繁体   English

如何使用管道有条件地显示innerHTML值

[英]how to conditionally display the innerHTML values using pipe

I am using angular 8. Wanted to conditionally implement the innerHTML using pipe translate.我正在使用 angular 8。想要使用管道翻译有条件地实现innerHTML

.html .html

<button type="button"     
    mat-flat-button
    // using translate module internally
   [innerHTML] = "display ? (HIDE_NUMBER' : SHOW_NUMBER) | translate)"
   (click)="toggle()">
</button>

.ts .ts

//all imports are done

   export class New implements OnInit {

    public display: boolean;

    constructor() {}

    toggle() {
      this.display = !this.display;
    }

}

You have a strange ' after HIDE_NUMBER :) .你有一个奇怪的'HIDE_NUMBER :) 之后。 But that's not the only problem但这不是唯一的问题

You need to do something like this你需要做这样的事情

[innerHTML] = " (condition ? 'translate_string'
                   : 'the_other_translate_string'
                ) | translate
                "

The idea is to wrap the condition inside () and then translate the result from that condition.这个想法是将条件包装在() ,然后从该条件转换result That's why the translate pipe is outside the ()这就是为什么translate管道在()之外

Try like this:像这样尝试:

<button type="button"     
      mat-flat-button
      [innerHTML] = "(display ? 'HIDE_NUMBER' : 'SHOW_NUMBER') | translate"
       (click)="toggle()">
</button>

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

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