I found many examples of calling a controller function from a directive, but could not find an example of a call from a directive template.
Let's say I have this HTML code to open a modal directive
<button ng-click='toggleModal()'>Open</button> <modal-dialog show='modalShown' confirm="confirmCtrl()"> <p>Modal Content Goes here<p> </modal-dialog>
Here is my controller with confirmCtrl () function I want to call:
myApp.controller('myController', function($scope){ $scope.modalShown = false; $scope.toggleModal = function() { $scope.modalShown = !$scope.modalShown; }; $scope.confirmCtrl = function () { alert('confirmed'); }
})
And here is my directive. I
.directive('modalDialog', function(){ return { restrict: 'E', scope: { show: '=', corfirm: '&' }, replace: true, transclude: true, link: function(scope, element, attrs) { scope.hideModal = function() { scope.show = false; }; }, template: "<div class='ng-modal' ng-show='show'><div class='ng-modal-overlay' ng-click='hideModal()'></div><div class='ng-modal-dialog' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='hideModal()'>X</div><div class='ng-modal-dialog-content' ng-transclude></div><button ng-click=""> Confirm </button></div></div>"
};
In my template, I have a button and I want to call the confirmCtrl () function on click, however I could not figure out how to do this
The fiddle works here http://jsfiddle.net/anao4nsw/
angularjs
Clementine
source share