2016-03-28 6 views
0

ng-repeatからカスタムディレクティブに値を設定しようとしています。しかし残念ながら、それはリピータ内の値を設定していませんが、リピータの設定値の外はその設定値です。ng-repeat内にangleディレクティブの値を設定する

HTML:にconsole.logで

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>

私は "d.id d.name"(文字列そのもの)ではない値を取得しています。これを修正する方法を教えていただけますか?

アドバンス

答えて

1

あなたははい、あなたは正しいです

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

ソリューションをありがとう! –

0

あなたはスコープにパラメータを結合しているおかげで、なぜあなたは代わりに$scope.id$scope.nameのあなたのディレクティブ内$attrs.id$attrs.nameをしますか?

+0

などのようなディレクティブ内の値ではない$attrため$scopeを使用する必要があります。それは私の間違いでした。 –

関連する問題