How can I get my directive to call to parent's controller function? I have a directive that look like this:
HTML:
<div ng-app="myapp" ng-controller="mainController as mainCtrl">
some body text<br>
<a href="javascript:;" ng-click="mainCtrl.myfunc()">Click Me from body</a>
<photo photo-src="abc.jpg" caption="This is so Cool" />
</div>
Javascript:
var app = angular.module('myapp',[]);
app.controller('mainController', function(){
var vm = this;
vm.myfunc = myfunc;
function myfunc(){
console.log("Log from myfunc");
}
});
app.directive('photo',function(){
return {
restrict: 'E',
template: '<figure> <img ng-src="{{photoSrc}}" width="500px"> <figcaption>{{caption}}</figcaption> <a href="javascript:;" ng-click="mainCtrl.myfunc()">Click Me</a></figure>',
replace: true,
scope: {
caption: '@',
photoSrc: '@'
}
}
});
When I click on Click Me from body link, it shows the console log message as expected. However, nothing happen when I click on Click Me from directive. How can I make it works?
&to pass functions in to yourscope, the same way you used@to pass other parameters.