【问题标题】:angular 2 - ngFor dynamic functionangular 2 - ngFor 动态函数
【发布时间】:2018-10-20 23:44:06
【问题描述】:

我有一个表格组件,它采用输入 JSON 作为表格格式,采用另一个 JSON 作为数据。我的问题是,当我的表格使用 *ngFor 渲染时,如何调用 cellFunction?

我的表格格式 JSON:

tblFormat= [
        { headerTxt: 'Order ID', df: 'Order_ID', color: 'blue', cellFunction: 'testME1' },
        { headerTxt: 'Buyer Name', df: 'name', color: 'blue',cellFunction: 'testME2' }
]

我的组件

import { Component, AfterViewInit, ViewChild, Input } from '@angular/core';

@Component({
    selector: 'table-comp',
    template: `<table class="table table-hover">
    <thead>
        <tr>
            <th *ngFor="let h of tableInfo"  [style.color]="h.color">{{h.headerTxt}}</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let d of data">
            <td *ngFor="let c of tableInfo" [style.color]=" how do I? c.cellFunction()">
                {{d[c.df]}}
            </td>
        </tr>
    </tbody>
 </table>`
    inputs: ['data','tableInfo']
})

export class TableComp {
    tableInfo=[];
    data=[];
} 

【问题讨论】:

  • 您想调用c.cellFunction() 使用[style.color] 分配颜色?
  • 什么是cellFunction?它是一个函数,还是如图所示的字符串?
  • c[cellFunction]() 如果你想打电话给testME1()
  • @baao 我假设该函数是在他的组件中定义的。我不认为单元格具有 Function 作为属性,尽管它可以!
  • 哈哈。是啊,你说得对。最后看起来真的很奇怪...... :-) @sabithpocker。

标签: javascript angular angular2-directives angular2-components


【解决方案1】:

主要取决于TestME1 和TestME2 的定义位置。如果是TableComp,可以试试:

 <td *ngFor="let c of tblFormat" [style.color]="this[c.cellFunction]()">

如果函数在其他地方定义,您只需将this 替换为定义它的对象。

例如,您可以拥有一个包含您注入组件的所有颜色函数的服务:

class TableComp {
    constructor(public colorFunctions: ColorFunctions) {}
}

如果你有这样的服务,你可以这样做:

<td *ngFor="let c of tblFormat" [style.color]="colorFunctions[c.cellFunction]()">

其实,我不知道你到底想做什么!

【讨论】:

  • 我在想如果在父组件中定义了 cellfunciton 我会怎么称呼它
  • 为什么不将它们移动到可以在任何地方注入的服务中,对于如何解析颜色值是否依赖于父级?你能提供一个带有 ParentComponet 代码的颜色解析器函数示例吗?
  • 向孩子注入父母作为参考并不是任何人都会推荐的。 Read this
【解决方案2】:

我已经完成了这项工作,请检查此链接 -> https://stackblitz.com/edit/angular-xwsq2a

在组件中添加colorChose方法,并像这样更新模板,

<td *ngFor="let c of tblFormat" [style.color]="colorChose(c.cellFunction)">

在组件中,在colorChose方法中

colorChose (param) {
  return this.executeFunctionByName(param, this);  
}

在同一个组件中创建executeFunctionByName 方法。当您知道函数名称为string 时,这用于调用函数。

executeFunctionByName(functionName, context /*, args */) {
  var args = Array.prototype.slice.call(arguments, 2);
  var namespaces = functionName.split(".");
  var func = namespaces.pop();
  for(var i = 0; i < namespaces.length; i++) {
    context = context[namespaces[i]];
  }
  return context[func].apply(context, args);
}

希望你有cellFunction中提到的方法,在本例中是testME1,它返回颜色名称,

testME1 () {
  return "red";
}

希望对你有帮助...

【讨论】:

  • 为什么 colorChose 不只是 "this[param].bind(this)()" 而不是那些乱七八糟的东西 ;)
  • @patrick,是的,您提到的声明效果很好。 executeFunctionByName 方法处理的场景更多,所以我认为最好将其包含在内。
  • 你的意思是什么场景?我想不出来,这不是我提到的陈述所处理的
  • @patrick,一种情况是在executeFunctionByName 的function_name 参数中使用namespace,即在参数中使用some_object.function_name。即this.executeFunctionByName(some_object.function_name, this); 。应该在some_object 的上下文中执行的函数。此函数负责处理此类情况,但是对于此问题,情况并非如此。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-07
  • 1970-01-01
  • 2017-09-20
  • 2018-02-06
  • 1970-01-01
相关资源
最近更新 更多