Angular4.x学习——*ngFor与*ngIf的使用

<el-menu model="1" background-color="#1b2637" text-color="#ffffff" active-text-color="#0066ff">
  <!--此处ng-container标签是一个虚拟的标签,不会被渲染,是用来避免*ngFor和*ngIf放在同一标签上冲突-->
  <ng-container *ngFor="let item of menuArr">
    <el-menu-item *ngIf="!item.subMenu.length" [index]="item.index" class="menu-item">
      <i [class]="item.iClass"></i>{{item.name}}
    </el-menu-item>

    <el-submenu *ngIf="item.subMenu.length" [index]="item.index" >
      <ng-template #title>
        <i [class]="item.iClass"></i>{{item.name}}
      </ng-template>
      <el-menu-item *ngFor="let subItem of item.subMenu" [index]="subItem.index" class="menu-item">
        <i [class]="subItem.iClass"></i>{{subItem.name}}
      </el-menu-item>
    </el-submenu>
  </ng-container>
</el-menu>

如图所示:

1、在angular中*ngFor和*ngIf不能放在同一标签上使用,否则会发生冲突。因此,使用ng-container标签来避免二者的冲突。ng-container标签不会被渲染在页面中,就像vue中的template标签一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明致成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值