javascript - Accessing the controller associated with a directive instance -
i have associated controller directive so:
return function mydirective() { return { scope: {}, restrict: 'e', template: template, controller: 'mycontroller', replace: true, }; };
if want access method on controller template, need add controller property on scope?
template:
<div> <div> <button ng-click="dosomething()">do something.</button> </div> </div>
controller:
function mycontroller() {} mycontroller.prototype.dosomething() { window.alert('foo'); }
you should avoid scope: {}
directive access controller functions because of scope: {}
in directive create isolate scope controller.
that's why may can not access controller functions directive template.
after avoid scope: {}
use functions normal controller functions.
like:
<button data-ng-click="myfunction()">call function</button>
you can use scope in link function in directive.
link: function (scope, element, attrs)
Comments
Post a Comment