2013-12-07 13 views
12

बनाएं मैं यूट्यूब वीडियो एम्बेड करने के लिए निम्नलिखित AngularJS निर्देश बनाया:एक यूट्यूब AngularJS निर्देशक

// 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>' 
    }; 
}); 

जब मैं इसे अपने टेम्पलेट <youtube code="r1TK_crUbBY"></youtube> से कहते हैं, मैं निम्नलिखित त्रुटि मिलती है:

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 के साथ, आपको और trustAsResourceURLsrciframe के इंजेक्ट करने की आवश्यकता है।

डेमो: http://plnkr.co/edit/0N728e9SAtXg3uBvuXKF?p=preview

.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); 
      } 
     }); 
    } 
    }; 
}); 

यह भी देखें: Migrate from 1.0 to 1.2 और related question

+0

मैंने उपर्युक्त कोशिश की और मैंने टेम्पलेट के अंत के लिए गायब कॉमा जोड़ा: लाइन, अब यह कोई त्रुटि नहीं दे रहा है, लेकिन यह उस वीडियो का निरीक्षण नहीं करता है, जिसे मैं एक्ट्री रिक्त खाली देखता हूं। –

+0

@ जोनाथनहिंडी यह यहां काम करता है: http://plnkr.co/edit/0N728e9SAtXg3uBvuXKF?p=preview कोणीय 1.2 के साथ आप टेम्पलेट में निर्देश का उपयोग कैसे कर रहे हैं? –

+0

धन्यवाद अब यह काम करता है, मैं <यूट्यूब कोड = "{{कोड}}"> का उपयोग कर रहा था, मैंने सोचा कि मुझे वर्तमान में मूल्यांकन किए जाने वाले वर्तमान ब्रैकेट के साथ इसे पास करने की आवश्यकता है, फिर निर्देश के वास्तविक मूल्य को पास करें। लेकिन ऐसा लगता है कि मैं गलत था। –

संबंधित मुद्दे