2016-12-06 1 views
0

이 간단한 예제는 내가이 질문을 위해 작성한 것입니다. 지시어를 사용하고 템플릿의 지시문 속성에 객체를 전달할 수 있는지 궁금합니다. templateUrl이 아닙니다.. 나는 다음과 같이 작동 할 것이라고 생각합니다 :템플릿 기능에서 지침을 사용할 수 있습니까?

angular.module('myModule') 
    .directive('someDirective', function() { 
    return { 
     scope: { 
     u: '=', 
     }, 
     template: '<avatar user="u"></avatar>', 
    }; 
    }); 

답변

0

예! 정의 순서는 중요하지 않습니다.

app.directive("directive1", function() { 
    return { 
     restrict: 'E', 
     scope: { 
      u: '@' 
     }, 
     template : "<h4>{{u}}</h4><directive2 user='{{u}}'></directive2>" 
    }; 
}); 
app.directive("directive2", function() { 
    return { 
     restrict: 'E', 
     scope: { 
      user: '@' 
     }, 
     template : "<h1>{{user}}</h1>" 
    }; 
}); 

jsfiddle

+0

답장을 보내 주셔서 감사합니다. 속성을 전달하도록 예제를 확장 할 수 있습니까? 가급적 대상물 – Jesse

+0

확실! 그것을 지금보십시오. – driconmax

+0

테스트를 쉽게하고 결과를보기 위해 문자열을 사용했습니다. 객체를 사용하여'@'를'='으로 변경하고 무엇을 인쇄 할 것인지 (예 :'u.name') – driconmax

0

행동에서 네, 범위 속성을 통해 상태를 통과하기 쉽습니다을 참조하십시오. 개념을 보여주는 plunkr이 있습니다.

app = angular.module('app',[]) 
    .directive('someDirective', function() { 
    return { 
     scope: { 
     u: '=', 
     }, 
     template: '<avatar user="u"></avatar>', 
    }; 
    }).directive('avatar', function() { 
    return { 
     scope: { 
     user: '=', 
     }, 
     template: '<span>{{user.name}}</span>', 
    }; 
    }) 
관련 문제