Questions tagged «angular-ng-if»

15
* ngIf和* ngFor在同一元素上导致错误
我在与试图使用角度的问题*ngFor,并*ngIf在相同的元素。 当尝试遍历中的集合时*ngFor,该集合被视为null,因此在尝试访问其在模板中的属性时失败。 @Component({ selector: 'shell', template: ` <h3>Shell</h3><button (click)="toggle()">Toggle!</button> <div *ngIf="show" *ngFor="let thing of stuff"> {{log(thing)}} <span>{{thing.name}}</span> </div> ` }) export class ShellComponent implements OnInit { public stuff:any[] = []; public show:boolean = false; constructor() {} ngOnInit() { this.stuff = [ { name: 'abc', id: 1 }, { name: 'huo', …

8
* ng如果在模板中,否则
我如何在一个*ngIf陈述中处理多个案件?我已经习惯的Vue或角1与具有if,else if和else,但似乎角4只具有true(if)和false(else)状态。 根据文档,我只能执行以下操作: <ng-container *ngIf="foo === 1; then first else second"></ng-container> <ng-template #first>First</ng-template> <ng-template #second>Second</ng-template> <ng-template #third>Third</ng-template> 但我想有多个条件(类似): <ng-container *ngIf="foo === 1; then first; foo === 2; then second else third"></ng-container> <ng-template #first>First</ng-template> <ng-template #second>Second</ng-template> <ng-template #third>Third</ng-template> 但是我最终不得不使用ngSwitch,这感觉像是一种hack: <ng-container [ngSwitch]="true"> <div *ngSwitchCase="foo === 1">First</div> <div *ngSwitchCase="bar === 2">Second</div> <div *ngSwitchDefault>Third</div> …

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.