19

I have a scope like $scope.doc_details in my angularjs controller, and I want to use it to display a pdf file by using a tag, like this:

<object data="{{doc_details.file_url}}" type="application/pdf"></object>

but it doesn't get loaded in the browser, in my chrome console, all I see is:

Failed to load resource: the server responded with a status of 404 (Not Found) 
http://localhost/%7B%7Bdoc_details.file_url%7D%7D

and when I just display it using <span>{{doc_details.file_url}}</span> it show the value.


The problem here is that the browser is seeing

<object data="{{doc_details.file_url}}" type="application/pdf"></object>

in the DOM before Angular compiles it, and obviously {{doc_details.file_url}} isn't a valid URL.

Directives can be your friend here.

Say we want to write:

<pdf src='doc_details.file_url'></pdf>

We can create a directive:

app.directive('pdf', function() {
    return {
        restrict: 'E',
        link: function(scope, element, attrs) {
            var url = scope.$eval(attrs.src);
            element.replaceWith('<object type="application/pdf" data="' + url + '"></object>');
        }
    };
});

This will defer the creation of the object element until we actually have the URL available to us from the scope (assuming it's already there - otherwise you'd want to $watch the src attribute on the scope until it became available).

Here this is in a fiddle.

4

4 に答える 4

34

As of AngularJS 1.1.4, you can use the ng-attr- prefix for the data attribute:

<object ng-attr-data="{{doc_details.file_url}}" type="application/pdf"></object>

See the ngAttr for binding to arbitrary attributes section on AngularJS: Interpolation and data-binding.

于 2014-02-13T11:10:54.360 に答える
25

ここでの問題は、ブラウザが見ていることです

<object data="{{doc_details.file_url}}" type="application/pdf"></object>

Angular がコンパイルする前に DOM で、明らかに{{doc_details.file_url}}有効な URL ではありません。

ディレクティブはここであなたの友達になることができます。

書きたいとしましょう:

<pdf src='doc_details.file_url'></pdf>

ディレクティブを作成できます。

app.directive('pdf', function() {
    return {
        restrict: 'E',
        link: function(scope, element, attrs) {
            var url = scope.$eval(attrs.src);
            element.replaceWith('<object type="application/pdf" data="' + url + '"></object>');
        }
    };
});

これにより、スコープから実際に URL を使用できるようになるまで、要素の作成が延期objectされます (URL が既に存在すると仮定します。そうでない場合は、使用可能になるまでスコープ$watchsrc属性を使用する必要があります)。

これはフィドルにあります。

于 2013-02-15T21:51:48.890 に答える
1

Thank you satchmorun, but if smb want change link without reloading page or modal you can use:

<pdf2 src="pdfUrl" height="heightForPDF"></pdf2>

And this directive:

app.directive('pdf2', ['$compile', function ($compile) {
    return {
        restrict: 'E',
        scope: {
            src: "=",
            height: "="
        },
        link: function (scope, element, attr) {
            function update(url) {
                    element.html('<object data="' + url + '" type="application/pdf" width="100%" style="height: ' + scope.height + 'px">' +
                        'Для просмотра pdf:<br /> Для Internet Explorer установите <a target="_blank" href="http://get.adobe.com/reader/">Acrobat Reader</a><br />' +
                        'Для Chrome: <a target="_blank" href="https://support.google.com/chrome/answer/6213030?hl=ru">Проверьте настройки</a>' +
                        '</object>');
                    $compile(element.contents())(scope);
            }
            scope.$watch('src', update);
        }
    };
}]);

Thank you Jerry from this answer and example of recompile a dynamically loaded directive.

p.s. "pdfUrl" and "heightForPDF" are variable in scope

于 2019-09-10T09:37:05.207 に答える
0

The browser tries to process the thing before AngularJS replaced the angulary expression with something the browser can work with, causing an error.

Adding an ng-cloak fixes the issue.

于 2018-03-21T09:26:14.217 に答える