How to set an iframe src attribute from a variable in AngularJS

后端 未结 6 1117
旧时难觅i
旧时难觅i 2020-11-22 17:24

I\'m trying to set the src attribute of an iframe from a variable and I can\'t get it to work...

The markup:

相关标签:
6条回答
  • 2020-11-22 17:42

    I suspect looking at the excerpt that the function trustSrc from trustSrc(currentProject.url) is not defined in the controller.

    You need to inject the $sce service in the controller and trustAsResourceUrl the url there.

    In the controller:

    function AppCtrl($scope, $sce) {
        // ...
        $scope.setProject = function (id) {
          $scope.currentProject = $scope.projects[id];
          $scope.currentProjectUrl = $sce.trustAsResourceUrl($scope.currentProject.url);
        }
    }
    

    In the Template:

    <iframe ng-src="{{currentProjectUrl}}"> <!--content--> </iframe>
    
    0 讨论(0)
  • 2020-11-22 17:42

    You need also $sce.trustAsResourceUrl or it won't open the website inside the iframe:

    angular.module('myApp', [])
        .controller('dummy', ['$scope', '$sce', function ($scope, $sce) {
    
        $scope.url = $sce.trustAsResourceUrl('https://www.angularjs.org');
    
        $scope.changeIt = function () {
            $scope.url = $sce.trustAsResourceUrl('https://docs.angularjs.org/tutorial');
        }
    }]);
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <div ng-app="myApp" ng-controller="dummy">
        <iframe ng-src="{{url}}" width="300" height="200"></iframe>
        <br>
        <button ng-click="changeIt()">Change it</button>
    </div>

    0 讨论(0)
  • 2020-11-22 17:47

    select template; iframe controller, ng model update

    index.html

    angularapp.controller('FieldCtrl', function ($scope, $sce) {
            var iframeclass = '';
            $scope.loadTemplate = function() {
                if ($scope.template.length > 0) {
                    // add iframe classs
                    iframeclass = $scope.template.split('.')[0];
                    iframe.classList.add(iframeclass);
                    $scope.activeTemplate = $sce.trustAsResourceUrl($scope.template);
                } else {
                    iframe.classList.remove(iframeclass);
                };
            };
    
        });
        // custom directive
        angularapp.directive('myChange', function() {
            return function(scope, element) {
                element.bind('input', function() {
                    // the iframe function
                    iframe.contentWindow.update({
                        name: element[0].name,
                        value: element[0].value
                    });
                });
            };
        });
    

    iframe.html

       window.update = function(data) {
            $scope.$apply(function() {
                $scope[data.name] = (data.value.length > 0) ? data.value: defaults[data.name];
            });
        };
    

    Check this link: http://plnkr.co/edit/TGRj2o?p=preview

    0 讨论(0)
  • 2020-11-22 17:57

    It is the $sce service that blocks URLs with external domains, it is a service that provides Strict Contextual Escaping services to AngularJS, to prevent security vulnerabilities such as XSS, clickjacking, etc. it's enabled by default in Angular 1.2.

    You can disable it completely, but it's not recommended

    angular.module('myAppWithSceDisabledmyApp', [])
       .config(function($sceProvider) {
           $sceProvider.enabled(false);
       });
    

    for more info https://docs.angularjs.org/api/ng/service/$sce

    0 讨论(0)
  • 2020-11-22 17:57

    this way i follow and its work for me fine, may it will works for you,

    <iframe class="img-responsive" src="{{pdfLoc| trustThisUrl }}" ng-style="{
                    height: iframeHeight * 0.75 + 'px'
                }" style="width:100%"></iframe>
    

    here trustThisUrl is just filter,

    angular.module("app").filter('trustThisUrl', ["$sce", function ($sce) {
            return function (val) {
                return $sce.trustAsResourceUrl(val);
            };
        }]);
    
    0 讨论(0)
  • 2020-11-22 18:08

    Please remove call to trustSrc function and try again like this . {{trustSrc(currentProject.url)}} to {{currentProject.url}}. Check this link http://plnkr.co/edit/caqS1jE9fpmMn5NofUve?p=preview


    But according to the Angular Js 1.2 Documentation, you should write a function for getting src url. Have a look on the following code.

    Before:

    Javascript

    scope.baseUrl = 'page';
    scope.a = 1;
    scope.b = 2;
    

    Html

    <!-- Are a and b properly escaped here? Is baseUrl controlled by user? -->
    <iframe src="{{baseUrl}}?a={{a}&b={{b}}"
    

    But for security reason they are recommending following method

    Javascript

    var baseUrl = "page";
    scope.getIframeSrc = function() {
    
      // One should think about their particular case and sanitize accordingly
      var qs = ["a", "b"].map(function(value, name) {
          return encodeURIComponent(name) + "=" +
                 encodeURIComponent(value);
        }).join("&");
    
      // `baseUrl` isn't exposed to a user's control, so we don't have to worry about escaping it.
      return baseUrl + "?" + qs;
    };
    

    Html

    <iframe src="{{getIframeSrc()}}">
    
    0 讨论(0)
提交回复
热议问题