ngSrc doesn't work with <embed>
The following doesn't work because browsers don't react to the change of src attribute after the initial load
<object>
<embed ng:src="{{video.embedUrl}}" type="application/x-shockwave-flash" allowscriptaccess="always"
allowfullscreen="true" width="450" height="274">
</embed>
</object>
http://www.codingforums.com/showthread.php?t=35800
one solution that comes to my mind:
we should create a embed widget that will correct this behavior for ng:src by doing the following if embed has ng:src attribute:
- remove self from the document dom
- start watching ng:src expression
- add a clone (or replace if already exists) with the correct src attribute and value every time ng:src evaluates to a new thing
Hey Igor, I'm running into trouble with this. Is there a fix?
Actually, I've got it figured out, I'll submit a pull request when I get some time.
Has this been resolved in any version yet?
Uf, this is a show stopper. Is there any work around? I can't find a way to use
My directive for this is:
app.directive('embedSrc', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var current = element;
scope.$watch(attrs.embedSrc, function () {
var clone = element
.clone()
.attr('src', attrs.embedSrc);
current.replaceWith(clone);
current = clone;
});
}
};
})
<embed embed-src="...">
It's probably a bit hacky but it works.
An embed element is still needed. This was raised 2 years ago, is it going to be in any update soon?
@phuu that worked for me, with one modification - the first parameter to $watch(...) needs to be a function that returns attrs.embedSrc:
scope.$watch(function() { return attrs.embedSrc; }, ...
This problem also occurs with object elements
@phuu - great suggestion! It worked fine for me with the minor modification from @kolosy.
It would be great to have this in a release. I understand that might not be used as often as ng-src, but there's still some people who would benefit from this tag. Is there anything wrong with @phuu's code?
@phuu would you mind putting together a PR?
This is an issue with video tag as well. And this is indeed still an issue, and has been for 3 years now.
I don't believe you actually need to do this (this being cloning elements) for the video/audio tags. We can use the DOM api to make this work correctly (I have a project I started a while ago called ng-media which aims to provide some of these niceties for audio/video tags in a similar fashion).
Some of it could be a tiny bit complicated (like dealing with multiple sources and multiple tracks and video overlays), but not all of that is needed for the core probably.
Anyways, http://jsfiddle.net/g3zWW/ here's a basic example of how it would work.
It might be a bit tricky to change ng-src itself for this, but could easily write some element directives for the video and audio tags to use the native api for this.
Anyways, there's no need to clone elements for this in any browsers that I'm aware of. (although the demo won't work in FF due to the codecs not being supported, and the video only having a single source)
@caitp, I think you are right, this works for me:
app.directive('embedSrc', function () {
return {
restrict: 'A',
link: function(scope, element, attrs) {
scope.$watch(
function() {
return attrs.embedSrc;
},
function() {
element.attr('src', attrs.embedSrc);
}
);
}
};
});
Still, this is an issue that should be fixed within the AngularJS library. If ng-src works for images, I imagine it should work for other elements as well.
I don't think we need to change the DOM attribute value since ng-src already does that, this seems to be an issue with the src and currentSrc properties not reflecting the attribute value. This may vary from browser to browser.
Anyways, it's pretty trivial to tell ng-src to set the property value as well, however this is hard to test without checking media files into the tree, which nobody wants to do (currentSrc isn't set unless the url is resolved, according to the spec)
@caitp, I agree, my solution is a workaround for my code, not something that I'm looking to commit to the AngularJS codebase.
Angular strips out the src or ng-src attributes if they contain templating but does not populate anything back in, giving me the impression that it does the stripping-out in a loose manner but the populating in a more stringently-defined manner. I would naively assume that Angular would just look at attribute names (ng-src for example) and not actually check the tag name, so I don't know if it should be necessary to check media into the AngularJS repository for testing. But if I had more time and knowledge of the Angular codebase I would try and solve the problem myself.
(If necessary, however, there is a 200k MP4 file here http://support.apple.com/kb/ht1425 which should be suitable for testing)
You mention that "src" and "currentSrc" properties are not reflecting the attribute value and that this may vary from browser-to-browser. I am curious why this would be the case, since I would assume that we are doing simple DOM manipulation and not actually doing anything media-specific?
Yeah, I'm actually talking about html5 media elements, and not referring to embed stuff. I guess I got confused by this issue.
I wonder if this is already working by default then checks
http://plnkr.co/edit/IstWRCI47Y6q9en7pfqB?p=preview yeah, I guess it's already working, unless there's some IE specific bug for that.
In that case I don't really have any idea how it's working with embed stuff, or how to approach resolving it. That's too bad!
No worries, thanks for the interesting discussion, I learned a bit more about how things work behind the scenes. I admit I don't know enough about Angular to submit a PR myself. I'll continue using the workaround for now.
I encountered the same issue a few weeks ago and this was my workaround:
http://jsfiddle.net/HB7LU/1504/
It's a surprisingly simple solution. I can create an ngEmbed directive basing on this.
I extended the object element into a directive so that it can be used like this:
<object ng-data="animation.swf" ng-param="{
'bgcolor': '#000000'
}"></object>
Which will expand into:
<object data="animation.swf">
<param name="bgcolor" value="#000000">
<embed src="animation.swf">
</object>
Commit can be found at a4204bd88bb4dd2b6802ddc3210671a7bf53d743.
Live demo can be found at http://jsfiddle.net/HB7LU/1552/.
Allegedly this is the best cross-browser way markup-wise. I've tested the directive in BrowserStack and this works for IE9+.
A few things to consider:
-
Some ugly hack is required to make this work in IE8 and I do not plan to do that, so if we'd like to have this solution as is, it'll have to wait until 1.3.x.
-
I honestly have no idea about how to unit test this and tips or helps are welcomed!
-
Lastly, my 2¢ - perhaps it's a good idea for @angular team to re-evaluate the necessity of adding this feature into the core module. This issue was initially raised 3 years ago, and we no longer use
<object>or<embed>that much in practice. For sound tracks and movies we now have<audio>and<video>, so the only valid use for<object>and<embed>remains to be Flash - whose usage has been in great decline as well [1][2]. Not to mention that most Flash use cases I could think of would not involve dynamically changing content files; the most likely one I could come up is an ads system.To summarize, I'd argue it's a better idea to put this feature into somewhere other than the core module (say
ngExtra), or I can make it available as a 3rd-party module, so that the core module is not becoming too bloated with features that only a very handful few (and still shrinking) would need.On the other hand the
srcissue with<video>and<audio>should be addressed (if they still haven't been) but in separate issues.