Aurelia:如何调用自定义元素之外的函数?

Chu*_*Nam 11 javascript aurelia

我有一个名为custom-element的自定义元素,我把它放在模板A中(带有控制器A)

定制元素

export class CustomElem {
  @bindable onCompleted;
  ........
}
Run Code Online (Sandbox Code Playgroud)

而updateDescription()是控制器A的一个功能.

export class A {
  updateDescription(){
    ....
  }
}
Run Code Online (Sandbox Code Playgroud)

如何使用custom-element调用updateDescription()?

Jer*_*yow 22

使用callbinding命令提供对自定义元素的函数调用的引用:

<custom-element on-completed.call="updateDescription()"></custom-element>
Run Code Online (Sandbox Code Playgroud)

updateDescription使用参数调用方法,可以执行以下操作:

export class CustomElem {
  @bindable onCompleted;

  ...

  fooBarBaz() {
    var args = {
      something: 'A',
      somethingElse: 'B',
      anotherArg: 'C'
    };
    this.onCompleted(args);
  }
}
Run Code Online (Sandbox Code Playgroud)
<custom-element on-completed.call="updateDescription(something, somethingElse, anotherArg)"></custom-element>
Run Code Online (Sandbox Code Playgroud)

  • 是 - 使用有关如何执行此操作的信息更新答案 (2认同)

val*_*hek 2

export class A {
  updateDescription = () => {
  };
}
Run Code Online (Sandbox Code Playgroud)

然后

<custom-element on-completed.bind="updateDescription"></custom-element>
Run Code Online (Sandbox Code Playgroud)

内部CustomElem通话this.onCompleted()