2013-12-07 2 views
12

을 만들어 YouTube 동영상을 삽입하려면 다음 AngularJS와 지침을 만들어 :유튜브 AngularJS와 지침

내 템플릿 <youtube code="r1TK_crUbBY"></youtube>에서 호출
// A Simple youtube embed directive 
.directive('youtube', function() { 
    return { 
    restrict: 'EA', 
    scope: { code:'=' }, 
    replace: true, 
    template: '<div style="height:400px;"><iframe style="overflow:hidden;height:100%;width:100%" width="100%" height="100%" src="http://www.youtube.com/embed/{{code}}" frameborder="0" allowfullscreen></iframe>' 
    }; 
}); 

, 나는 다음과 같은 오류 얻을 :

Error: [$interpolate:noconcat] Error while interpolating: http://www.youtube.com/embed/ {{code}} Strict Contextual Escaping disallows interpolations that concatenate multiple expressions when a trusted value is required. See http://docs.angularjs.org/api/ng .$sce

내가 할 수있는을 {{code}} 표현에서 지시어에 무엇이 잘못된 것인지 식별하지 못합니다.

답변

32

각도가 1.2 인 경우 $sce servicetrustAsResourceURLsrciframe으로 삽입해야합니다.

데모 :

.directive('youtube', function($sce) { 
    return { 
    restrict: 'EA', 
    scope: { code:'=' }, 
    replace: true, 
    template: '<div style="height:400px;"><iframe style="overflow:hidden;height:100%;width:100%" width="100%" height="100%" src="{{url}}" frameborder="0" allowfullscreen></iframe></div>', 
    link: function (scope) { 
     scope.$watch('code', function (newVal) { 
      if (newVal) { 
       scope.url = $sce.trustAsResourceUrl("http://www.youtube.com/embed/" + newVal); 
      } 
     }); 
    } 
    }; 
}); 

http://plnkr.co/edit/0N728e9SAtXg3uBvuXKF?p=preview

또한 참조 : Migrate from 1.0 to 1.2related question을.

+0

나는 위의 시도하고 템플릿 끝에 대한 누락 된 쉼표를 추가했습니다 : 줄, 지금은 어떤 오류를 제공하지 않지만 포함 된 비디오를 표시하지 않는, 내가 볼 수있는 요소를 검사 src attr 비어 있습니다. –

+0

@JonathanHindi 여기에서 작동합니다 : http://plnkr.co/edit/0N728e9SAtXg3uBvuXKF?p=p 각도와 각도 1.2 템플릿에서 지시문을 어떻게 사용하고 있습니까? –

+0

고마워 지금은 작동합니다, 나는 을 사용하고 있었는데, 먼저 대괄호를 사용하여 그것을 평가 한 다음 실제 값을 지시문에 전달해야한다고 생각했습니다. 하지만 내가 잘못한 것 같습니다. –

관련 문제