加载ngFor中的最后一项后调用指令 [英] Calling a directive after the load of last item in ngFor

查看:72
本文介绍了加载ngFor中的最后一项后调用指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a component which contains a table, I call ngFor on a row inside the tBody, the problem is that I need the directive that I apply on the Table to be run after loading the last element of 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"传递给了函数,但是当我在控制台上记录接收到的值时,它是未定义的

Here, I tried multiple stuff the last of which made the most sense where I passed the "last" in the ngIf to a function but when I logged the received value on the console it was undefined

在角度1中,我达到了想要使用的目的

in angular 1 I achieved what I wanted using

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

推荐答案

一种方法是将指令应用于所有行并传递last.在last为true的情况下,该指令实际上会执行某些操作.

An approach would be to apply a directive to all rows and pass last. Where last is true the directive actually does something.

@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.

You could also create a custom ngFor that provides additional features.

这篇关于加载ngFor中的最后一项后调用指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆