【问题标题】:TS file value in ngIf conditionngIf 条件下的 TS 文件值
【发布时间】:2021-03-20 23:34:00
【问题描述】:

如何将 ts 文件中的值转换为 HTML 文件中的 ngIf 条件? 我试过带或不带大括号,但它似乎不起作用。 sender_id 是数字类型。提前致谢!

.html 文件

*ngIf="item.Sender_ID != {{sender_id}}"

ts.文件

this.api.getStaffDetailsByUsername(this.username).subscribe(res=>{
      console.log(res)
      this.department = res[0].Departament;
      this.sender_id = res[0].Staff_ID;

【问题讨论】:

  • .ts 文件是角度组件吗? .html 是组件模板?
  • 它应该只是*ngIf="item.Sender_ID != sender_id"。在[...]*... 内部,一切都是表达式,而不是字符串。所以不需要插值

标签: javascript angular ionic-framework


【解决方案1】:

一旦一个字段在 ts 文件中被声明为 public(如果没有使用 # 或 private 或 protected,这是默认情况),该字段也可以在 html 代码中使用。只需要在表达式“{{ .. }}”之外。在 *ngIf 指令中,它也可以直接作为 item 使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-24
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2019-01-27
    相关资源
    最近更新 更多