2013-04-21 14 views
19

Wie kann eine Anweisung eine Funktion von einem Controller mit einigen Parametern aufrufen?Aufruf einer Controller-Funktion aus der Direktive mit Parametern

Ich würde die Variable MyVar dem Geltungsbereich geben. $ Apply (attrs.whattodo);

HTML:

<div ng-app="component"> 
    <div ng-controller="ctrl"> 
    <span ng-repeat="i in myarray"> 
    <span customattr whattodo="addVal">{{i}}</span> 
    </span> 
    </div> 

-Controller JS:

function ctrl($scope) { 
     $scope.myarray = [1]; 
     $scope.addVal = function (value) { 
      $scope.myarray.push(value); 
     } 
    } 

Richtlinie JS:

angular.module('component', []).directive('customattr', function() { 
    return { 
     restrict: 'A', 
     link: function (scope, element, attrs) { 
      var myVar = 5; 
      scope.$apply(attrs.whattodo); 
     } 
    }; 
}); 

Antwort

17

Hier ist einer der Arbeitsmethoden:

Sie haben dieses Attribut in Rahmen als Rahmen-Modell mit Funktionstyp zu binden. So können Sie dies aus, wenn Sie in anderen (Richtlinie) müssen sope

HTML

<body ng-controller="MainCtrl"> 
    Value: {{value}}! 

    <button customattr whattodo="addValue">Add</button> 
</body> 

JS

angular.module('component', []) 

.controller('MainCtrl', function($scope) { 
    $scope.value = 1; 

    $scope.addValue = function(val){ 
    alert(val); 
    $scope.value = val; 
    } 
}); 

.directive('customattr', function() { 
    return { 
     restrict: 'A', 
     scope: { 
      whattodo: "=" // or ' someOtherScopeName: "=whattodo" ' 
     }, 
     link: function (scope, element, attrs) { 
      var myVar = 5; 
      scope.whattodo(myVar); // or ' scope.someOtherScopeName(myVar) ' 
     } 
    }; 
}); 

Hier ist code on plunker

fromAngularJS: Directives

= or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localModel:'=myAttr' }, then widget scope property localModel will reflect the value of parentModel on the parent scope. Any changes to parentModel will be reflected in localModel and any changes in localModel will reflect in parentModel

+0

Das funktioniert super danke. Eine letzte Sache: Das Beispiel, das ich gab, war ein dumy. Aber mit meinem "echten" Beispiel: der Wert ist eine Liste von Dicts, die ich mit ein wenig Eingabetext rendere. Wenn ich den leeren Bereich oder den von Ihnen angegebenen Bereich einfüge, werden alle meine Eingabefelder leer. Irgendeine Idee warum?Danke – JohnCastle

+0

Ehrlich gesagt schwer vorstellbar, wäre es nicht kompliziert für Sie, Ihr reales Beispiel in Plunker zu bewegen? Danke – Maksym

+0

Natürlich! Hier ist es: http://plnkr.co/edit/mu9DM1dEyZ36UqVvw0DM Setzen Sie den Geltungsbereich in der Direktive auf true und Sie werden "Toto" darin sehen. Oder – JohnCastle

10

in html

whattodo="addVal(value)" 

in Richtlinie

scope.$apply(function(s){ 
    s.whattodo({value : myVar}); 
}); 
+0

Ich habe: Typeerror: Anwesen 'whattodo' von Objekt # ist keine Funktion? – JohnCastle

+0

Entschuldigung. Ich habe es korrigiert. sollte an whattodo sein – Ketan

+0

Sehr interessant und funktioniert gut ([demo] (http://jsfiddle.net/BinaryMuse/rhXDs/)). Nach einigem Nachdenken muss das Gleiche in Dingen wie '

2

Warum verwenden Sie das Zeichen "&" nicht isoliert?

<body ng-controller="MainCtrl"> 
    Value: {{value}}! 
    <button customattr add-val="addValue(value)">Add</button> 
</body> 

In Controller:

function ctrl($scope) { 
     $scope.myarray = [1]; 
     $scope.addValue = function (value) { 
      $scope.myarray.push(value); 
     } 
    } 

Und in Richtlinie:

angular.module('component', []).directive('customattr', function() { 
    return { 
     restrict: 'A', 
     scope: { 
      addVal: "&" 
     }, 
     controller: function ($scope) { 
      var myVar = 5; 
      // To execute addVal in controller with 'value' param 
      $scope.addVal({value: value}) 
     } 
    }; 
}); 
+1

in Direktive wo immer du addVal benutzt hast, benutze stattdessen whattodo – userRaj

Verwandte Themen