Skip to content

Instantly share code, notes, and snippets.

@huangzhuolin
Created August 2, 2018 10:11
Show Gist options
  • Save huangzhuolin/b1b89880d763f6ce8b92cc28bce54d51 to your computer and use it in GitHub Desktop.
Save huangzhuolin/b1b89880d763f6ce8b92cc28bce54d51 to your computer and use it in GitHub Desktop.
[pass function as parameter to component in anglularjs] #angularjs
// ...
export class ComponentCtrl {
// ...
onClick() {
// We can access outer scope function here with the binding name.
this.outerHandleClick({param1: 1, param2: 2}) // We can also pass parameters to outer function.
}
}
<button ng-click="ctrl.onClick()">hello</button>
import { coreModule } from "app/core/core";
import { ComponentCtrl } from "...";
coreModule.component("MyComponent", {
templateUrl: "example.component.html",
controller: ComponentCtrl,
controllerAs: "ctrl",
bindings: {
outerHandleClick: "&", // bind function
},
});
<my-component outerHandleClick="ctrl.someFunction(param1, param2)"></my-component>
// ... this is the outer controller
someFunction(param1, param2) {
console.log(param1, param2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment