Angular: directive with callback - 14493050
http://angularjs.org/
HTML
<div ng-controller="myController">
<button my-directive listener-callback="testCallback();">button label</button>
</div>
JavaScript
( function() {
var myApp = angular.module( "myApp", [ "anotherModule" ] );
myApp.directive( "myDirective", ['$timeout', function( $timeout ) {
return {
restrict: "A",
scope: { listenerCallback: '&' },
link: function( scope, element, attr ) {
// At an event I will call listenerCallback();
// Mocking with timeout for now:
$timeout(function(){
// Waited long enough, let's use that callback:
scope.listenerCallback();
}, 500);
}
};
}]);
var anotherModule = angular.module( "anotherModule", [] );
anotherModule.controller( "myController", [ "$scope", function( $scope ) {
$scope.testCallback = function(){ console.log('called back') };
// Here i'd like to access the addListener function in myDirective
} ] );
} )();