Created
August 2, 2018 10:11
-
-
Save huangzhuolin/b1b89880d763f6ce8b92cc28bce54d51 to your computer and use it in GitHub Desktop.
[pass function as parameter to component in anglularjs] #angularjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
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. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button ng-click="ctrl.onClick()">hello</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { coreModule } from "app/core/core"; | |
import { ComponentCtrl } from "..."; | |
coreModule.component("MyComponent", { | |
templateUrl: "example.component.html", | |
controller: ComponentCtrl, | |
controllerAs: "ctrl", | |
bindings: { | |
outerHandleClick: "&", // bind function | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<my-component outerHandleClick="ctrl.someFunction(param1, param2)"></my-component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... 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