angularjs - 如何在枚举中使用 ng-if 条件? (angular2/ typescript )

标签 angularjs typescript angular enums

我有一个 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/

相关文章:

javascript - 如果子数组具有动态键值对对象,则在 JavaScript 中展平数组对象

typescript - TS4090 错误 : Conflicting libraries for node in electron

Angular:在子路由上设置特定的路由链接处于事件状态

angular - 使用 angularfire2 和 firestore 创建自动完成搜索?

javascript - 指令中的 UI Bootstrap 模态 - 多个模态但只有一个打开

angularjs - 如何以编程方式在 Ui.grid angularJs 中选择一行?

javascript - 如何使用 Protractor 在 Canvas 上的特定位置执行单击事件

javascript - 在指令中调用时,Angular JS Controller 元素未定义

javascript - 在 JavaScript 中合并并创建唯一区分大小写的数组

Angular webpack编译成功,卡在编译阶段-网页打不开