angular2中ViewChild,索引值相關

2017/8/24
時間

1.知識點一:angular2中的@ViewChild與@ViewChildren

ViewChild屬性裝飾器,用來在模板視圖中獲取匹配的元素

@ViewChild(TableComponent);
table:TableComponent

ViewChildren用來匹配多個元素,返回的結果是一個QueryList集合

@ViewChildren(TableComponent);
tables:QueryList<TableComponent>

2.知識點二:ng2 獲數組中元素的索引值
文檔中提供的方法NgForOf
用法:

<li *ngFor="let user of userObservable | async as users; index as i; first as isFirst">
   {{i}}/{{users.length}}. {{user}} <span *ngIf="isFirst">default</span>
</li>

在項目中的實際應用:
我這裏使用的是kendoUI,應用場景是切換選項卡,每個tab裏又有各自的添加事件

<p-tabPanel  *ngFor="let item of items;let i = index" [header]="item.name">
  <button kendoButton (click)="add($event,item,i)" >添加</button>
</p-tabPanel>

其中的let i = index 即獲取到了數組中元素的索引值。

3.知識點三:根據索引值獲取該條數據/component

  add(event,item:any,i){
  const table =  this.tables.toArray()[i];
  table.editor.add();
  }

其中的i就是獲取的index值。

2017n8-25
1.知識點一:chrome F12調試
其中Network面板中的Headers和Preview
今天項目中的實例 是在做添加功能時,保存進去的數據沒有id值,在Headers中中查看請求 在Preview中查看返回值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章