I have an AngularJS directive:
myApp.directive(\'movie\', function(){
return {
restrict: \'E\',
replace: true,
scope: { product:\'=\', codebase:
I ran into a similar issue trying to embed a PDF via directive. The problem is that when the template is placed into the DOM the bindings have not yet been interpolated, as @mfelix supposes. However it is trickier because object
invokes code outside the browser so it isn't guaranteed to play well with invalid or dynamic attributes (depends on the plugin I suppose).
I wound up having to write a link
function, which $observes
the variable, just as in the ng-src
/ng-href
solution. In the case of ng-src
or ng-href
, the $observe
callback on the variable simply sets the corresponding attribute and everything works because HTML5 (or as we used to call it, DHTML) is cool like that. For example, you might have an <a>
tag without a corresponding href
. The browser handles this just fine. And when you set an href
on it after the first Angular digest, the browser can deal with it. But for the <object>
tag, it doesn't work so well, because even if the plugin is misconfigured (say, missing src attribute), the browser has no way of knowing that and it will still invoke the corresponding plugin, which will treat missing information in a highly idiosyncratic way. In my case, Firefox handled it gracefully and Chrome barfed.
So the next approach I tried was to use the $observing
callback to inject a child element containing the fully specified object
tag (concatenating the variable into the template string using the + operator).
Simplified example directive definition:
scope: ...
link: function(scope, element, attrs) {
attrs.$observe('src', function(value) {
if (value) {
element.html(
'<object width="100%" type="application/pdf" data="'+value+'">');
} else {
element.html("<div></div>"); // We have to put something into the DOM
}
});
},
etc: ...
When src
finally has a value, and when its value changes, the callback you register via $observe will be invoked.
My third solution, which I like best, was to process the PDFs into GIFs and display them as images, completing forgoing accursed plugins (and no, PDF.js wasn't cutting it). So maybe you can turn your Flash movies into animated .gifs. That would be awesome.