angularjs - Call function from directive without isolated scope -


i have read this post. however, in example calls controller function after listening on click event of element.

how can achieve calling controller function when clicking children of directive element?

    <div ng-controller="myctrl">     <abc method1="outermethod('c')" method2="outermethod2('g')"></abc> </div> 

directive:

var myapp = angular.module('myapp',[]);  myapp.directive('abc', function() {     return {         restrict: "ea",         replace: true,         template: "<div><p ng-click='clickedp()'>p</p><div ng-click='clickeddiv()'>div</div></div>",         controller: function($scope) {             // how can call outermethod if clickedp executed???             // how can call outermethod2 if clickeddiv executed???         },         controlleras: "vm",         link: function(scope, element, attrs, vm) {          }     } });   function myctrl($scope) {     $scope.outermethod = function( ) {         alert( "you did it" );     }         $scope.outermethod2 = function( ) {         alert( "you did again" );     } } 

fiddle: http://jsfiddle.net/j93ba7a2/5/

the scope can used directly without passing attributes. also, using "controlleras" on directive same value parent controller bad idea, since overwrite it.

solution:

var myapp = angular.module('myapp', []);  myapp.directive('abc', function () {     return {         restrict: "ea",         replace: true,         template: "<div><p ng-click='clickedp()'>p</p><div ng-click='clickeddiv()'>div</div></div>",         controller: function ($scope) {              // how can call outermethod if clickedp executed???             $scope.clickedp = function () {                 $scope.outermethod(); // call it!             }             // how can call outermethod2 if clickeddiv executed???             $scope.clickeddiv = function () {                 $scope.outermethod2(); //same way!             }          },         controlleras: "vm",         link: function (scope, element, attrs, vm) {              /* have been better handle clickedp ,                 clickeddiv here instead of in controller, i'm                 trying avoid confusion changing little                 possible of code. */          }     } });  function myctrl($scope) {     $scope.outermethod = function (a) {         alert("you did it");     }     $scope.outermethod2 = function (a) {         alert("you did again");     } 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -