2016-07-29 1 views
1

Hier Code:In eckigen, wie eine Funktion in einem Controller der Direktive definiert?

Richtlinie Code:

angular.module('app', ['localytics.directives', 'ngLoadScript']) 
 
.directive("home", function() { 
 
\t return { 
 
\t \t restrict: "A", 
 
\t \t replace: true, 
 
\t \t template: "<div ng-include='var'>{{var}}</div>", 
 
\t \t controller: function($scope) { 
 
\t \t \t //loading home page - as default 
 
\t \t \t $scope.var = "/tfm/home.html" 
 
\t \t \t //on change the page changed dynamically! 
 
\t \t \t $scope.change = function(where) { 
 
\t \t \t \t $scope.var = where; 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 
})

ICH WILL Chanage ANRUFEN (wo) FUNKTION DER RICHTLINIE - KONTROLLER der Richtlinie.

Controller-Code:

.controller('wToEatController', ['$scope', function($scope) { 
 
\t $scope.submitInfoAboutTable = function() { 
 
\t \t //validation for time selection 
 
\t \t if($scope.idealTableTime == undefined || $scope.rightTableTime == undefined) { 
 
\t \t \t return; 
 
\t \t } 
 
\t \t 
 
\t \t //Booking details to be updated - here users preference is updating! 
 
\t \t var bookingDetails = { 
 
\t \t \t idealTableWaitTime: $scope.idealTableTime, 
 
\t \t \t rightTableWaitTime: $scope.rightTableTime 
 
\t \t } 
 
\t \t 
 
\t \t //Let's update booking information - the booking key will be same used while login, public/tfm.html 
 
\t \t FirebaseDbService.updateBooking(function(isUpdated) { 
 
\t \t \t console.log(isUpdated); 
 
\t \t \t //I WANT TO CALL chanage(where) function of DIRECTIVE 
 
\t \t \t $scope.change = "change('/tfm/home.html')"; 
 
\t \t }, bookingDetails, bookingKey); 
 
\t } \t 
 
}]);

Ist es möglich?

+0

ein Dienst nicht – YOU

+0

Service machen Richtlinie nicht funktioniert, die nicht Vorlage festgelegt wird: Vorlage: „

{{var}}
“ –

+0

inject diesen Dienst sowohl Richtlinie und Controller – YOU

Antwort

2

Sie haben ein Attribut zu erstellen, mit dem der Link wird (in diesem Beispiel customAttr) erfolgen:

<span yourDirectiveName customAttr="myFunctionLink(funcInDirective)"></span> 

Und in der Richtlinie Controller nur gesetzt, wie im folgenden Ausschnitt das neue Attribut ('&' zwei Art und Weise der Datenbindung), und eine Verbindung mit Ihrer Richtlinie Methode erstellen:

scope : {customAttr : '&'}, 
link : function(scope,element,attrs){ 

     scope.myDirectiveFunc = function(){ 
      console.log("my directive function was called");} 
     } 
     scope.customAttr({funcInDirective : scope.myDirectiveFunc}); 
} 

und in Ihrem Controller:

$scope.myFunctionLink = function(funcInDirective){ 
$scope.callableDirectiveFunc = funcInDirective;} 

Jetzt können Sie Ihre Richtlinie Funktion aufrufen mit $scope.callableDirectiveFunc();

+0

Danke, das hat mir Stefan geholfen! –

+0

Froh ich könnte helfen :) –

Verwandte Themen