2016-03-28 5 views
0

Ich versuche, Werte von ng-repeat auf eine benutzerdefinierte Anweisung zu setzen. Leider setzt es nicht den Wert innerhalb des Repeaters, sondern außerhalb des Repeaters seinen Einstellwert.Legen Sie den Winkelrichtwert innerhalb von ng-repeat fest

HTML:

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

 
app.controller('testController', function ($scope) { 
 

 
$scope.data = [ 
 
\t \t \t \t {"id": 1, "name": "Image Link"}, 
 
       {"id": 2, "name": "Image Upload"}, 
 
       {"id": 3, "name": "Video Link"}, 
 
       {"id": 4, "name": "Video Upload"} 
 
];  
 
\t \t 
 
}); 
 

 
app.directive('heartCounterDir', ['$http', function($http) { 
 
    
 
return { 
 
\t restrict: 'E', 
 
\t transclude: true, 
 
\t replace: true,  
 
\t scope:{ 
 
\t \t id:'=', 
 
\t \t name:'=', 
 
\t }, 
 
\t controller:function($scope, $element, $attrs){ 
 

 
\t \t $scope.heartCounterAction = function(){ \t 
 
\t \t \t 
 
\t \t \t console.log($attrs.id , $attrs.name); 
 

 
\t \t \t }; 
 
\t $scope.heartCounterAction(); 
 

 
\t }, 
 
\t 
 
\t template: '<div></div>' \t 
 
}; 
 

 
}]);
<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <meta charset="UTF-8"> 
 
\t \t <title></title> \t 
 
\t \t <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script> 
 
\t \t \t <script src="app.js"></script> 
 
\t \t </head> 
 
\t \t 
 
\t <body> 
 
\t <div ng-app="testApp" ng-controller="testController"> 
 
\t \t <div ng-repeat="d in data"> 
 
\t \t <div> the id is : {{d.id}} and the name is : {{d.name}}</div> 
 
\t \t <heart-counter-dir id="d.id" name="d.name" > 
 
\t \t \t 
 
\t \t </heart-counter-dir> \t 
 

 
\t \t </div> 
 
\t \t 
 
\t \t 
 
\t </div>

im console.log Ich erhalte "d.id d.name" (die Zeichenfolge selbst) nicht den Wert. Kannst du mir bitte sagen, wie ich das beheben kann?

Vielen Dank im Voraus

Antwort

1

Sie müssen wie $scope für Werte nicht $attr innerhalb Richtlinie verwenden, wie

return { 
    restrict: 'E', 
    transclude: true, 
    replace: true,  
    scope:{ 
     id:'=', 
     name:'=', 
    }, 
    controller:function($scope, $element, $attrs){ 

     $scope.heartCounterAction = function(){ 

      console.log($scope.id , $scope.name); 

      }; 
    $scope.heartCounterAction(); 

    }, 

    template: '<div></div>' 
}; 
+0

Danke für die Lösung! –

0

Sie den Umfang der Parameter verbindlich, also warum tun Sie $attrs.id und $attrs.name in Ihrer Richtlinie statt $scope.id und $scope.name?

+0

ja, du hast recht. Es war mein Fehler . –