angular - 动态(点击)功能 - Angular2

标签 angular

我在将动态函数绑定(bind)到点击事件时遇到了一些麻烦。请看下面:-

文件 1

<title-bar [actions]='[{title: "Create A New Plan", link: "hello"}]' ></title-bar>

文件 2

<div class="actions" *ngIf="actions">
    <a *ngFor="let action of actions" class="ui primary button" (click)="[action.link]()">{{action.title}}</a>
</div>

除了我在(单击)中绑定(bind) action.link 之外,所有代码都运行良好。

如果我创建以下按钮:-

<button (click)="hello()"></button>

它按原样调用 hello() 函数。但出于某种原因,我无法让它动态工作。

对于这个我可能忽略了的问题,有人有简单的解决方案吗?

该函数调用一个简单的警报只是为了测试:-

public hello() {
    alert('test');
}

我曾尝试将点击事件更改为以下内容,但没有成功:-

(click)="action.link"
(click)="[action.link]()"
(click)="this[action.link]()"

我分别得到以下错误:-

No error, but not function called
inline template:3:2 caused by: ["hello"] is not a function
inline template:3:2 caused by: self.parentView.parentView.context[self.context.$implicit.link] is not a function

我们将不胜感激任何插入正确方向的帮助。

最佳答案

在你需要的组件中

get self() { return this; }

在模板中

<a *ngFor="let action of actions" class="ui primary button"
  (click)="self[action.link]()">{{action.title}}</a>

<罢工>

使用

<a *ngFor="let action of actions" class="ui primary button"
  (click)="this[action.link]()">{{action.title}}</a>

关于angular - 动态(点击)功能 - Angular2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42298258/

相关文章:

angular - ngx-translate 在延迟加载的模块中没有显示任何文本

html - 在选择选项中添加图标 - Angular 2

angular - 如何修复错误 : ASSERTION ERROR: Should be run in update mode [Expected=> false == true <=Actual]

使用 Visual Studio 2015 无法在 Angular 2 上编译 Typescript 文件

Angular2 火力地堡 : Response for preflight has invalid HTTP status code 405

javascript - 使用 Enter/Space 键单击 <a> 元素不起作用,即使它已聚焦(可访问性)

angular - 激活一次的路由器命名 socket

javascript - ng2-ckeditor 在 angular2 应用程序中不起作用?为什么?

angular - 类型 'Observable<>' 在 Angular Nativescript 中缺少属性

javascript - 从浏览器/Javascript 调用本地 PowerShell 脚本