2017-11-01 3 views
0

나는 그것이 NG 반복 지시자 내부에 정의되어 자체의 요소 지시어에 정의 된 개체 속성이 정의되지 않은 객체 :각도 지침 속성 속성이

<div ng-repeat="element in array"> 
    <my-directive el-sel="{{element}}> 
     <something else> 
    </my-directive> 
</div> 

을 그리고 이것은 myDirective입니다 :

app.directive('myDirective',function(){ 
    return { 
    restrict:'E', 
    scope: false, 
    link: function($scope,$element,$attrs){ 

     console.log('element:' + JSON.stringify($attrs.elSel)); 
     console.log('href: ' + $attrs.elSel.href); 

    } 
    } 
}); 

콘솔 결과는 다음과 같습니다

element:"{\"name\":\"a name\",\"href\":\"#something\"}" 
href: undefined 

누군가가이 동작하고 뭐를 설명시겠습니까 내가 잘못하고있는거야?

+0

당신이 구문 분석 시도해 봤어':

여기
console.log('href: ' + JSON.parse($attrs.elSel).href); 

이 지침에 객체를 전달하는 최소한의 예입니다 console.log ($ attrs.elSel);'단순히 객체가 문자열로 올지 또는 다른 데이터 유형으로 오는지 여부를 확인합니다. –

답변

1

문자열로 {{element}}을 전달 중입니다.이 값은 {{variable}}입니다.

단기적으로

,이 문제를 해결할 것입니다

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

 
app.controller('MainCtrl', function($scope) { 
 
    $scope.something = { 
 
    name: 'a name', 
 
    href: '#somewhere' 
 
    }; 
 
}); 
 

 
app.directive('myDirective', function() { 
 
    return { 
 
    restrict: 'E', 
 
    scope: { 
 
     elSel: '=' 
 
    }, 
 
    link: function($scope, $element, $attrs) { 
 
     console.log($scope.elSel) 
 
    } 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app="app" ng-controller="MainCtrl"> 
 
    <my-directive el-sel="something"> 
 
    </my-directive> 
 
</div>

+0

고마워요. 그러니 한 가지 더 질문을 드리 자마자 곧 답을 수락하겠습니다. ng-repeat의 객체를 참조로 전달하는 방법이 있습니까? 중괄호가 없기 때문에 – Typo

+0

예제를 사용하여 내 대답을 편집했습니다. 추가 읽기 [여기.] (https://docs.angularjs.org/guide/directive#isolating-the-scope-of-a-directive) – UncleDave