【问题标题】:Calling a directive after the load of last item in ngFor在 ngFor 中加载最后一项后调用指令
【发布时间】:2016-08-13 12:54:26
【问题描述】:

我有一个包含表格的组件,我在 tBody 内的一行上调用 ngFor,问题是我需要在加载 ngFor 的最后一个元素后运行在表格上应用的指令。

这是我的组件

import {Component} from 'angular2/core';
import {InfScrollTableService} from './inf-scroll-table.service'
import {HTTP_PROVIDERS} from 'angular2/http';
import {InfScrollTableDirective} from './inf-scroll-table.directive'
@Component({
  selector: 'my-app',
  template: `
   <table class="scrollable" infScrollTable>
     <thead>
       <tr>
         <th class="small-cell">Code</th>
         <th>Label</th>
         <th>Area</th>
       </tr>
     </thead>
     <tbody>
       <tr  *ngFor="#country of countries; #last=last" *ngIf="last">
         <td class="small-cell">{{country.id}}</td>
         <td><a href="#" >{{country.id}}</a></td>
         <td>{{last}}</td>
       </tr>
     </tbody>
   </table>
`,
providers: [InfScrollTableService, HTTP_PROVIDERS],
directives: [InfScrollTableDirective]
})

export class AppComponent {
  public countries;
  constructor(private _infScrollTableService: InfScrollTableService){
    this._infScrollTableService.getCountries()
    .subscribe(
      data => {
        console.log(data);
        this.countries=data;
      });
  }
}

我的指令:

import {Directive, ElementRef} from 'angular2/core'

@Directive({
  selector: '[infScrollTable]',
})

export class InfScrollTableDirective{
   private _elem:HTMLTableElement;

   constructor(el: ElementRef) { this._elem = el.nativeElement; console.log(el)}
   bord(last:boolean){
    if(!last){
      return
    }
    console.log(this._elem)
    var th = this._elem.tHead.rows[this._elem.tHead.rows.length - 1].cells;
    var td = this._elem.tBodies[0].rows[0].cells;
    var tdLen = td.length - 1;
    var j = 0;
    for (; j < tdLen; j++) {
      if (th[j].offsetWidth > td[j].offsetWidth) {
        td[j].style.width = th[j].offsetWidth + 'px';
        th[j].style.width = th[j].offsetWidth + 'px';
      } else {
        th[j].style.width = td[j].offsetWidth + 'px';
        td[j].style.width = td[j].offsetWidth + 'px';
      }
    }
  }
}

在这里,我尝试了多种方法,其中最后一种最有意义,我将 ngIf 中的“last”传递给函数,但是当我在控制台上记录接收到的值时,它是未定义的

在角度 1 中,我实现了我想要的使用

$scope.$on('LastElem', function(){...})

【问题讨论】:

  • 导致重新渲染的数据更改怎么办?目前,即使您仅在中间插入或删除也会发生这种情况(我想这将在未来进行优化)如果您将项目附加到 countries 怎么办?

标签: angular ngfor


【解决方案1】:

一种方法是将指令应用于所有行并传递last。如果last 为真,则指令实际上做了一些事情。

@Directive({selector: '[isLast]'})
export class LastDirective{
   @Input() isLast:boolean;
   @Output() lastDone:EventEmitter<boolean> = new EventEmitter<boolean>();
   ngOnInit() {
     if(this.isLast) {
       this.lastDone.emit(true);
     }
   }
}
<tr  *ngFor="let country of countries; let last=last" [isLast]="last" (lastDone)="doSomething()">

您还可以创建一个自定义 ngFor 来提供额外的功能。

【讨论】:

  • 无法绑定到“isLast”,因为它不是已知的原生属性
  • 您需要将指令或组件应用于具有@Input() isLast; 的元素,以便[isLast]="last" 工作。例如,这个 wozld 需要 directives: [LastDirective] 在您使用上述指令的 @Component() 装饰器中。
  • 已经将该指令导入并包含在指令中:[]
  • 我刚刚将 @Directive({selector: 'isLast'}) 更改为 @Directive({selector: '[isLast]'}) 并且它起作用了。我不知道这是否与新版本的更改有关。我正在使用 rc2
  • 我正在通过单击按钮重新填充国家/地区,并且我的 (lastDone) 触发了两次。你有什么想法吗?
猜你喜欢
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 2018-06-29
相关资源
最近更新 更多