angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ngSrc doesn't work with <embed>

Open IgorMinar opened this issue 15 years ago • 27 comments

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

IgorMinar avatar May 07 '11 00:05 IgorMinar

@phuu @kolosy Thank you guys so much! Worked beautifully. As of v1.2.13, I was still experiencing this issue and needed to use the workaround.

In addition, in my case I needed to embed a PDF and although the PDF's were now loading, Chrome was complaining "Could not load Chrome PDF Viewer." I was able to find a workaround by doing the following:

embed embed-src="{{url}}" src="{{url}} " type="application/pdf"

So although the src attribute doesn't really "do anything", it quiets the Chrome error and now everything works great.

The final code (phuu's original with kolosy's edit) if anyone is confused:

.directive('embedSrc', function () {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      var current = element;
      scope.$watch(function() { return attrs.embedSrc; }, function () {
        var clone = element
                      .clone()
                      .attr('src', attrs.embedSrc);
        current.replaceWith(clone);
        current = clone;
      });
    }
  };
})

arvin-foroutan avatar Feb 23 '14 23:02 arvin-foroutan

is (still) necessary for loading Native Client modules.

steven-johnson avatar Aug 28 '14 01:08 steven-johnson

The above embedSrc directive worked for me. I was actually having a similar issue with using the object element for displaying a pdf. After setting up the embedSrc directive my final code below worked for displaying base64 encoded PDFs with Angular.

    <object type="application/pdf"
            width="100%"
            height="900"
            src="{{invoiceBased64}}"
            embed-src="{{invoiceBase64}}">
    </object>

cnaccio avatar Mar 18 '15 15:03 cnaccio

The solution above seems to work for me. However I've come across two issues.

  1. FF seems to want to load the initial src value before binding has occurred so i see requests like: http://example.com/{{invoiceBase64}}

Leaving the src attribute blank seems to cure that problem so that no request is made until the initial value is set on embed-src

<object type="application/pdf"
    src=""
    embed-src="{{invoiceBase64}}">
</object>
  1. Although the PDF is requested as expected initially, subequent requests (on change of embed-src) are firing twice. Not ideal. So far I haven't figured a way to stop this :( Any suggestions??

uberspeck avatar Jun 02 '15 18:06 uberspeck

I used below directive and it works for a single ngSrc embed object

myApp.directive('embedSrc', function (svc, $timeout) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var current = element;
            scope.$watch(attrs.embedSrc, function () {
                scope.$watch(svc.getMessage, function (v) {
                    $timeout(function () {
                        if (v != '') {
                            var rtspobj = angular.fromJson(v.content);
                            scope.cameraRTSPUrl = rtspobj.START_STREAMER_RESPONSE.RtspURL;
                            var clone = element.clone().attr('src', scope.cameraRTSPUrl);
                            current.replaceWith(clone);
                            current = clone;
                        }
                    });
                });
            });
        }
    };
});

But how do I make it work when I have multiple embed-src objects like below

<embed width="800" height="650" loop="no" autoplay="yes" version="VideoLAN.VLCPlugin.2" 
type="application/x-google-vlc-plugin" id="vlcCam1" embed-src="Cam1RTSP" src="">

<embed width="800" height="650" loop="no" autoplay="yes" version="VideoLAN.VLCPlugin.2" 
type="application/x-google-vlc-plugin" id="vlcCam2" embed-src="Cam2RTSP" src="">

Reference - http://stackoverflow.com/questions/38370121/angularjs-call-directive-dynamically-based-on-id-passed

Please Help !

slimshadddyyy avatar Jul 14 '16 10:07 slimshadddyyy

tweaked @tgvashworth solution a bit to make it work for Angular v1.0.1:

app.directive('embedSrc', function () {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      var current = element;
      var _url = $sce.trustAsResourceUrl(attrs.embedSrc);
      scope.$watch(_url , function () {
        var clone = element
                      .clone()
                      .attr('src', attrs.embedSrc);
        current.replaceWith(clone);
        current = clone;
      });
    }
  };
})

arpitaggarwal avatar Jul 02 '17 13:07 arpitaggarwal

Thanks everyone. I used a combination of the previous solutions suggested and that's what worked for me

app.directive('embedSrc', () => ({
    restrict: 'A',
    link(scope, element, attrs) {
        let current = element;
        scope.$watch(() => attrs.embedSrc, () => {
            const clone = element
                .clone()
                .attr('src', attrs.embedSrc);
            current.replaceWith(clone);
            current = clone;
        });
    },
}));

roytz avatar Aug 17 '18 00:08 roytz