2016-11-26 2 views
1

Wie in der Steuerung kann ich Funktion clearCopy von Direktive aufrufen?So rufen Sie die Funktion von der Direktive im Controller auf? AngularJS

Dies ist Teil meiner html:

<tr ng-form="mForm" my-directive> 
    <td> 
    <div> 
     <button class="btn btn-default" ng-click="saveData(row)"> </button> 
    </div> 
    </td> 
</tr> 

Diese meine Richtlinie ist:

angular.module("w.forms").directive("myDirective", function() { 
    return { 
     require: ["^form"], 
     link: function (scope, element, attrs, ctrls) { 
      scope.$watch(function() { 
      // ...... something 
      }, true); 

      scope.clearCopy = function() { 
       // do something 
      } 
     } 
    }; 
}); 

Das ist mein Controller ist:

angular.module("app").controller("datalesController", function ($scope) { 
    $scope.saveData(row) = function { 
    // do something then run function from directive 
    // till this part everything works fine 
    $scope.clearCopy() // unfortunately it doesn't work :(
    } 
} 

Alles funktioniert gut, außer Funktion $scope.clearCopy() in Controller funktioniert nicht.

+0

http://stackoverflow.com/questions/16881478/how-to-call-a-method-defined-in-an-angularjs- läuft Richtlinie könnte etwas Licht werfen. – Jason

+0

Ihr Anweisungsmodul und das Steuerungsmodul sind unterschiedlich. –

Antwort

-2

HTML

<html> 
<script src="library/angular.min.js"></script> 
<script src="practice.js"></script> 
<head> 
</head> 
<body ng-app="app" ng-controller="datalesController"> 
    <div my-directive> 
     <button ng-click="saveData()">press </button> 
    </div> 
</body> 
</html> 

Controller

angular.module('app',[]).controller("datalesController", function ($scope) { 
    $scope.saveData = function() { 
    // do something then run function from directive 
    // till this part everything works fine 
    $scope.clearCopy(); // unfortunately it doesn't work :(
    }; 
}); 

Richtlinie

angular.module('app',[]).directive("myDirective" , function() { 
    return { 
     restrict:'A', 
     link: function (scope, element, attrs, ctrls) { 
      scope.clearCopy = function() { 
       console.log("calling from controller"); 
      }; 
     } 
    }; 
}); 

ich Ihren Code ändern für Ihre Anfrage

+0

Sie haben das Modul geändert. Es ist nicht lösbar – DiPix

+0

Ich habe das Modul geändert, weil es verschiedene Module gibt, "w.forms" und "app". Zusätzlich zu Ihnen nicht einschränken: 'A' in Ihrer Richtlinie und saveData-Funktion wurde falsch definiert. – shahab

+0

Das ist der Punkt – DiPix

Verwandte Themen