【问题标题】:ngIf with depending from number binding image in Angular 4ngIf 取决于 Angular 4 中的数字绑定图像
【发布时间】:2017-05-10 08:44:49
【问题描述】:
 <td *ngFor="let user of userService.users | async">
 <div *ngIf="user?.data.apps.details[0].acknowledged as fooIcon">
 <img *ngIf="fooIcon === "1" " src="./app/img/icones_sized/tick.png"/>
 <img *ngIf="fooIcon === "0" " src="nothing_appears"/>
 </div> 

如果我的“.acknowledged”返回“1”并且如果它返回“0”,则我正在尝试显示图像,则不应显示任何内容...它适用于字符串但不适用于数字..我不真的不明白为什么..

【问题讨论】:

  • 您希望它同时处理字符串和数字吗?还是只有数字?
  • 你有嵌套的双引号,这根本不起作用。试试'1'
  • 只想使用数字作为 '0' = notthing 和 '1' = show img
  • 我已经尝试了 '1' 但它不起作用
  • 只用作&lt;img *ngIf="fooIcon === 1" src="./app/img/icones_sized/tick.png"/&gt;

标签: angular typescript ngfor angular-ng-if


【解决方案1】:

您应该使用 *ngIf...else,因为

    <ng-template #loading>
        <img src="./app/img/icones_sized/stable_arrow_small.png"/>
    </ng-template>
    <div *ngIf="user?.data.apps.details[0].acknowledged===1;else loading;">
      <img src="./app/img/icones_sized/tick.png"/>
    </div>

【讨论】:

  • 当然,也许他应该是,但为什么他的原始代码不起作用?
  • 只想使用数字作为 '0' = notthing 和 '1' = show the image.. 我在这篇文章中更新了我的代码
  • 我可以将“正在加载”设置为“无显示”还是为空,我的意思是如果返回“0”,视图中不会出现任何内容
  • 非常感谢.. ;) 它帮助我找到了另一种获取信息的方式,因为我希望在它返回 0 时显示任何 img ===>
  • @the end end I do so ===>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 2020-06-21
  • 2018-02-07
  • 2018-08-15
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
相关资源
最近更新 更多