ahsan ayub
ahsan ayub

Reputation: 307

Calling function of controller from directive when using isolated scope?

I want to call a function of Controller from Directive, It is for validation. But i'm a bit confused about how to call it from Directive when i'm using isolated scope. Here is the code of directive:-

App.directive('test',function(){
    return{
    require: "ngModel",
     scope:{
         a:"=",
         b:"=",
         c:'=',
         d:'='
            },
         link: function(scope, element, attributes,modelVal )
            {
            modelVal.$validators.test= function(val)
              {
                return false;
               //isolated scope values will make if condition true or false.
               if(scope.a=="true"){
                //I need to call a function of controller here. But how can 
                //i call that function from directive? this is my problem
                         } 
               }
             scope.$watch("a", function()
               {
                    modelVal.$validate();
                });

         }//end of link function;
      }//end of return;
});//end of directive;

Function is in my controller, i think i don't need to write the controller code. In my html , I'm calling this directive as:

     <test a="1" b="2" c="3" d="4" ng-model="abc"></test>

What changes i need to do in my directive to call the controller function which is $scope.testFunction()?

Upvotes: 0

Views: 53

Answers (2)

Santhosh V
Santhosh V

Reputation: 410

var module = angular.module('myModule', []);

module.controller('TestController', function($scope){
  $scope.text = 'test';
  
  // Will be invoked from 'test' directive
  $scope.alertMe = function(text){
    alert(text);
  };
});

module.directive('test', function(){
  return {
    restrict: 'E',
    template: '<input ng-model="text" ng-change="onChange(text)" />',
    scope: {
      text: '='
    },
    link: function(scope, elem, attr){
      scope.onChange = function(text){
      
      // Invoking parent controller function
       scope.$parent.alertMe(text);
      }
    }
  }
})
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myModule" ng-controller="TestController">
 
<test text="text"></test>

</div>
</body>
</html>

Upvotes: 1

Santhosh V
Santhosh V

Reputation: 410

I have given a sample, try like this.

Controller:

module.controller('TestController', function($scope){
  $scope.onTextChange = function(text){
    alert(text);
  };
})

HTML:

<test my-func="onTextChange(text)"></test>

Directive:

module.directive('test', function(){
  return {
    restrict: 'E',
    template: '<input ng-model="text" ng-change="onChange(text)" />',
    scope: {
      myFunc: '&'
    },
    link: function(scope, elem, attr){
      scope.onChange = function(text){
        if(typeof scope.myFunc === 'function'){
          // Calling the parent controller function
          scope.myFunc({text: text});
        }
      }
    }
  }
})

Upvotes: 0

Related Questions