1

Controller in page :

(function () {
 'use strict';
  angular.module('LVS').controller('LVSCtrl', LVSCtrl);
  function LVSCtrl($scope) {
   $scope.OnChange = function() {
   // do
   }
  }
 })();

This is my directive code My Directive code :

(function () {
  'use strict';

  angular.module('LVS')
      .directive('taEmp', taEmp);

  function taEmp() {
      return {
        restrict: 'E',
        scope: {
            ngModel: '=',
            ngDisabled: '=',
            ngReadonly: '=',
            ngChange: '&',
        },
        templateUrl: 'app/pages/ESS-TA/Common/Directives/TAEmpPicker.html',

    }

    })();

My Directive in page :

<ta-emp ng-model="empCode" ng-change="OnChange()"></ta-emp>

My directive not call function in controller

8
  • Please add your ta-emp directive. Commented Mar 5, 2017 at 16:03
  • My directive not call function in controller ... just a guess, but this might be because you don't have any code for your directive. Commented Mar 5, 2017 at 16:03
  • i already create this directive Commented Mar 5, 2017 at 16:08
  • Why and when do you think OnChange() should be called? Commented Mar 5, 2017 at 16:10
  • When empCode is changed Commented Mar 5, 2017 at 16:11

1 Answer 1

1

I made it work by using $watch inside your directive and parsing an controller function as param into it. The function gonna be executed once the input value has changed.

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

myApp.controller('MyCtrl', function ($scope) {
    $scope.name = '';
    $scope.someFunction = function (data) {
       console.log(data);
    };
});

myApp.directive('myDirective', function () {

  return {
      restrict: 'E',
      scope: {
        model: '=ngModel',
        function: '='
      },
      template: '<input type="text" ng-model="model" function="function" my-directive>',
      link: function (scope, element, attrs) {
         scope.$watch('model', function (newValue, oldValue) {
           if (newValue && newValue !== oldValue) {
             if (typeof scope.function === 'function') {
               scope.function('test');
             }
           }
         }, true);
      }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <my-directive ng-model="name" function="someFunction"></my-directive>
</div>

Sign up to request clarification or add additional context in comments.

7 Comments

@AthitUpakan glad to help ya!
why scope.function(); in directive can call function with parameter too? ex. $scope.OnChange = function(data) { //.... });
@AthitUpakan yep this is possible.
I'm newbie, do you have document or link for this solution? thanks again.
Be aware that using ng-model for an attribute name will instantiate the ngModelController and attach it to the element. Since the directive does not properly use the ngmodelController API, the ng-change directive will not work. Use of the ng- prefix is reserved for AngularJS core directives. Please avoid using it in custom directives.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.