我有一个 View ,在这个 View 中有 3 个按钮:
showListOne()
showListTwo()
showListThree()
当单击每个按钮时,我想在同一 View 但不同时显示不同的列表(一次只显示一个列表,就像选项卡行为一样)。
所以我想用枚举来做,就是这样:
export enum CurrentListView {人物、汽车、地点}
@Component({
selector: 'my-app',
styles: [require('./my-app.css')],
directives: [DND_DIRECTIVES],
providers: [DND_PROVIDERS],
template: require('./my-app.component.html')
})
@Injectable()
export class AppCmp implements OnInit {
listOfPeople: Person[];
listOfCars: Car[];
listOfPlaces: Place[];
currentListView: CurrentListView;
constructor(private _MyService: MyService) {
};
public showListOfPeopleData(): void {
this.currentListView = CurrentListView.People;
}
public showListOfCarsData(): void {
this.currentListView = CurrentListView.Cars;
}
public showListOfPlacesData(): void {
this.currentListView = CurrentListView.Places;
}
ngOnInit() {
this._MyService.getListOfPeopleData().subscribe(res => {
this.listOfPeople = res;
});
this._MyService.getListOfCarsData().subscribe(res => {
this.listOfCars = res;
});
this._MyService.getListOfPlacesData().subscribe(res => {
this.listOfPlaces = res;
});
}
}
现在,如何使用 ng-if 询问 currentListView
的状态?这是我的观点(ng-如果有我尝试过的并且有效):
<div *ngIf="currentListView == CurrentListView.People">
<div dnd-sortable-container [dropZones]="['zone-one']" [sortableData]="listOfPeople">
<div class="list-bg" *ngFor="#person of listOfPeople; #i = index" dnd-sortable [sortableIndex]="i">
ID: {{person.id}} <p></p> Age: {{person.age}}
</div>
</div>
</div>
<div *ngIf="currentListView == CurrentListView.Cars">
<div dnd-sortable-container [dropZones]="['zone-one']" [sortableData]="listOfCars">
<div class="list-bg" *ngFor="#car of listOfCars; #i = index" dnd-sortable [sortableIndex]="i">
ID: {{car.id}} <p></p> Color: {{car.color}}
</div>
</div>
</div>
<div *ngIf="currentListView == CurrentListView.Places">
<div dnd-sortable-container [dropZones]="['zone-one']" [sortableData]="listOfPlaces">
<div class="list-bg" *ngFor="#place of listOfPlaces; #i = index" dnd-sortable [sortableIndex]="i">
City: {{place.city}} <p></p> Capacity: {{place.capacity}}
</div>
</div>
</div>
我的 typescript 或 html 可以更有效率吗?我很乐意看到一个更通用的方法的建议,无需重复。
谢谢分配!
最佳答案
我不认为你可以比较数组(更不用说它的性能很差), 我建议您可以将“待显示列表”存储在某个字符串变量中 像这样:
currentListView:string = 'Bikes';
喜欢这个plunkr
关于angularjs - 如何在枚举中使用 ng-if 条件? (angular2/ typescript ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38677583/