Adrian Golian
Adrian Golian

Reputation: 41

AngularJS - how transform controller function to directive?

How can I transform angularjs controller function to a directive? I want to reuse it in other controllers. Nothing what I have tried is not working and I want to follow the right steps.

My code:

$scope.lockableFormWithCustomValidation = function(functionName, isFormValid) {

  if (isFormValid) {
    $scope.lockForm = true;
    if (angular.isFunction($scope[functionName])) {
      $scope[functionName]().then(function(response) {
        if (response.data.returnValue === 0) {
          return $scope.lockForm = true;
        } else {
          return $scope.lockForm = false;
        }
      });
    }
  }
};

I want to use it like:

... <form lockable-form="'createReport', addReportForm.$valid" ... >

Instead of:

   <form data-ng-submit="lockableFormWithCustomValidation('createReport', addReportForm.$valid)"

Upvotes: 0

Views: 43

Answers (1)

Satzz
Satzz

Reputation: 67

Better solution is , move it to service. then you can use it to other directives.

formvalidationService.js

angularApp.service('formValidationService',function(){
//To do logic here

});

Upvotes: 1

Related Questions