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

后端 未结 6 1177
旧时难觅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:

    
    

提交回复
热议问题