【问题标题】:call function for every row of *ngFor angular2为 *ngFor angular2 的每一行调用函数
【发布时间】:2017-07-05 03:03:38
【问题描述】:

我必须显示一个表格并为表格的每一行调用一个函数,并且该函数为一行调用一次。

<tr *ngFor="let item of mf.data"  >
          <td >
            <button (click)="remove(item)" class="btn btn-danger">x</button>
          </td>
          <td>{{item.name}}</td>
          <td>{{item.email}}</td>
          <td class="text-right">{{item.age}}</td>
          <td>{{item.city | uppercase}}</td>
        </tr>

请建议我将如何实现此功能?

【问题讨论】:

  • 你想要发生的事情
  • 上面的代码看起来是正确的,你得到了什么错误?
  • 我想为该 *ngfor 的每一行调用一个函数??

标签: angular typescript ngfor


【解决方案1】:

你可以添加指令

@Directive({ selector: '[invoke]'})
class InvokeDirective {
  @Output() invoke:EventEmitter = new EventEmitter();
  ngAfterContentInit() {
    this.invoke.emit(null);
  }
}

然后像这样使用它

<tr *ngFor="let item of mf.data" (invoke)="myFunction(item)" >

【讨论】:

  • Angular @Gunter 提供了什么内置功能??
  • 不这么认为。
  • 您能解释一下我将如何与我的组件集成吗?我是角度 2 的新手
  • 这是第二个代码被剪断。 myFunction(...) 是组件中的一个方法,当 *ngFor 添加 &lt;tr&gt; 时会调用该方法
  • 我添加了指令和函数,但它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
  • 2017-03-10
相关资源
最近更新 更多