Angular 2:如何让* ngIf使用包含它的* ngFor数组中的值?

时间:2017-05-17 08:31:15

标签: angular typescript ionic2

我是角度2和离子的新手,所以我会尽量缩短它:

 <ion-card class="acc-page-card" *ngFor="let account of accounts">
   <ion-card-content>
    <!-- Add card content here! -->
    <ion-item (click)="GoTo('AccountPage')">
        <div class="acc-img" item-left>
            <img src="{{account.img}}" alt="">
        </div>
        <div class="acc-details">
            <span class="name">{{account.title}}</span>
            <span class="title">{{account.link}}</span>
        </div>
        <div class="acc-icons" item-right>
            <i *ngIf="valueFromArray" class="icomoon-Add-user-icon"></i>
            <i *ngIf="valueFromArray" class="icomoon-Favorites-icon"></i>
        </div>
    </ion-item>
  </ion-card-content>

</ion-card>

其中&#39; valueFromArray&#39;是我想从循环的数组中得到的值 我试过了:

<i *ngIf="{{account.isFriend}}" class="icomoon-Add-user-icon active-icon"></i>

这是错的。 所以,如何使用数组中的值我在这样的情况下循环? 抱歉我的英语不好。

2 个答案:

答案 0 :(得分:5)

您只需像访问其他变量一样访问它:

<i *ngIf="account.isFriend" class="icomoon-Add-user-icon active-icon"></i>

img标签的另外几行应该是:

        <img [src]="account.img" alt="">

不要在内部属性中使用插值{{...}},只需在内容文本中使用。对于像src这样的属性值,您应该使用输入属性绑定[attribute]=value

答案 1 :(得分:2)

尝试使用* ngIf =“account.isFriend”

<i *ngIf="account.isFriend" class="icomoon-Add-user-icon active-icon"></i>