Angular 多个模板属性的问题

  • 看我的前端代码
        <th *ngFor="let item of cols" nzWidth="{{ item.width }}" nzAlign="{{ item.alignCaption }}"
          *ngIf="{{ item.visible }}">
          {{ item.caption }}
        </th>
  • 运行起来报错:
ERROR Error: Uncaught (in promise): Error: Template parse errors:
Can't have multiple template bindings on one element. Use only one attribute prefixed with (" <th ngFor="let item of cols" nzWidth="{{ item.width }}" nzAlign="{{ item.alignCaption }}" [ERROR ->]*ngIf="{{ item.visible }}">{{ item.caption }} </th>
  • 看出来是使用了多个*ngXxx的问题,但是要如何解决我的需求?
  • 通过*ngFor循环创建表格列头,其中根据属性item.visible动态设定该列头是否显示
  • 我使用了style.display:none,但是布局会错位(这里的none去掉了DOM元素的展位)
  • 还测试了style.visibility:hidden,这样会保留占位,结果是有空白出现了。。
  • 要如何达到我要的效果?
  • 有必要的话QQ:409223171
阅读 4.1k
2 个回答
<ng-container *ngFor="let item of [1, 2, 3]">
            <th *ngIf="item > 1">
                {{ item }}
            </th>
        </ng-container>

问题解决了,display 使用 compact 属性就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进