【问题标题】:Ionic & Angular: Inserting Function Names with ngForIonic & Angular:使用 ngFor 插入函数名称
【发布时间】:2019-03-05 08:59:51
【问题描述】:

我如何通过 *ngFor 插入一个函数调用,而这个调用是从我正在迭代的数组中插入的字符串?

我的数组包含由函数名称(以及其他名称)描述的用户操作列表。该模板有一部分应使用 Ionic 的 ion-fab 指令为每个用户条目列出这些操作。我不想写下每个操作,而是想使用 *ngFor 遍历列表并将每个函数名称插入到 (click) 属性中。
不过,我目前的解决方案不起作用。

这是我的代码:

constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private usersProv: Users
) {
    this.userActions = [
        {
            'label'     : 'Edit',
            'function'  : 'editUser',
            'icon'      : 'ios-create-outline',
            'color'     : 'primary'
        },
        {
            'label'     : 'Remove',
            'function'  : 'removeUser',
            'icon'      : 'ios-trash-outline',
            'color'     : 'yellow'
        },
        {
            'label'     : 'Send message',
            'function'  : 'sendMessageToUser',
            'icon'      : 'ios-send-outline',
            'color'     : 'secondary'
        }
    ];
    console.info('userActions', this.userActions);
}

模板

<ion-fab right>
    <button ion-fab mini color="light">
        <ion-icon name="ios-arrow-dropleft"></ion-icon>
    </button>
    <ion-fab-list side="left">
        <div *ngFor="let action of userActions">
            <button ion-fab mini color="{{action.color}}" title="{{action.label}}" (click)="action.function(user)">
                <ion-icon name="{{action.icon}}"></ion-icon>
            </button>
        </div>
    </ion-fab-list>
</ion-fab>

这是我得到的错误:

ERROR TypeError: "_v.context.$implicit.function is not a function"

插入大括号 ((click)="{{action.function}}(user)") 也无济于事。那么错误是:

`ERROR Error: "Uncaught (in promise): Error: Template parse errors: Parser Error: Got interpolation ({{}}) where expression was expected at column 0 in [{{action.function}}(user)]`

这可能吗?
是否可以按照我的方式进行处理?

提前致谢!

【问题讨论】:

  • 保存对函数的引用而不是字符串,例如'function' : this.editUser。请注意,没有括号,因为我们想要引用该函数,而不是调用它
  • 正如@user184994 所说,想要一个函数,而不是字符串。您不能在 JavaScript 或 Angular 模板中调用字符串,例如 "f"(1)
  • 为什么不创建一个像executeAction(action:string, user: any): void 这样的方法并在内部使用switch(action){ case 'editUser': this.editUser(user); break; ... } 调用正确的方法?

标签: arrays angular ionic-framework ngfor function-call


【解决方案1】:

你应该持有对函数的引用,而不是使用字符串,就像这样

userActions = [
    {
        'label'     : 'Edit',
        'function'  : this.editUser.bind(this),
        'icon'      : 'ios-create-outline',
        'color'     : 'primary'
    },
    {
        'label'     : 'Remove',
        'function'  : this.removeUser.bind(this),
        'icon'      : 'ios-trash-outline',
        'color'     : 'yellow'
    },
    {
        'label'     : 'Send message',
        'function'  : this.sendMessageToUser.bind(this),
        'icon'      : 'ios-send-outline',
        'color'     : 'secondary'
    }
];

这里我使用bind,所以我们在调用函数时不会丢失this 的上下文。

然后,在您的 HTML 中,您可以这样称呼它:

<button (click)="a.function(user)">{{a.label}}</button>

Here is a StackBlitz demo

【讨论】:

    【解决方案2】:

    我建议使用@user184994 的解决方案,绑定this 上下文并使用引用,但是如果您仍然想使用字符串,您可以通过this['methodName'](parameter) 访问组件的方法,在我的示例中它看起来像@987654324 @。

    这是STACKBLITZ

    【讨论】:

    • 请注意构建中的缩小/丑化,因为函数名称可能会更改,this[a.function] 将未定义
    • @user184994,你确定吗? a.function 是一个对象属性,它只会在组件中被缩小,而在模板中不会吗?我一直认为,在默认的 webpack 构建中,对象属性是不可触及的。
    • 所以a.function仍然会被设置为methodName,但是方法本身已经被丑化了,现在称为x来节省字节,因此this[a.function]正在寻找this.methodName , 不再存在
    • @user184994,顺便说一下,我们在当前项目中使用以下结构,并且它在很长一段时间内都适用于 dev 和 prod 构建,所以看起来丑化是安全的,我们没有这部分有任何错误
    • 啊好吧,这值得知道。我认为构建过程将 TS 类函数名称作为构建的一部分进行了丑化,但我刚刚检查过,正如你所说的那样很好
    猜你喜欢
    • 2018-10-16
    • 2021-10-02
    • 1970-01-01
    • 2019-10-20
    • 2019-11-10
    • 2020-05-22
    • 2020-10-25
    • 2018-10-20
    • 2023-03-15
    相关资源
    最近更新 更多