Home > Web Front-end > JS Tutorial > body text

How to Trust and Set iFrame Src Attribute from a Variable in AngularJS?

Patricia Arquette
Release: 2024-10-21 13:57:02
Original
894 people have browsed it

How to Trust and Set iFrame Src Attribute from a Variable in AngularJS?

Setting iFrame Src Attribute from a Variable in AngularJS

In AngularJS, you may encounter challenges when attempting to set the src attribute of an iFrame from a variable. To address this issue, we must delve into the provided code:

<br>function AppCtrl($scope) {</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$scope.projects = {

    1 : {
        "id" : 1,
        "name" : "Mela Sarkar",
        "url" : "http://blabla.com",
        "description" : "A professional portfolio site for McGill University professor Mela Sarkar."
    },

    2 : {
        "id" : 2,
        "name" : "Good Watching",
        "url" : "http://goodwatching.com",
        "description" : "Weekend experiment to help my mom decide what to watch."    
    }
};

$scope.setProject = function (id) {
    $scope.currentProject = $scope.projects[id];
    console.log( $scope.currentProject );

}
Copy after login

}

The crux of the issue lies in the absence of the trustSrc function referenced in the ng-src attribute. You need to inject the $sce service into the controller to utilize the trustAsResourceUrl method for sanitizing the URL string.

<br>function AppCtrl($scope, $sce) { // Injected $sce service</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// ...

$scope.setProject = function (id) {
  $scope.currentProject = $scope.projects[id];
  $scope.currentProjectUrl = $sce.trustAsResourceUrl($scope.currentProject.url);
  console.log( $scope.currentProject );
  console.log( $scope.currentProjectUrl );

}
Copy after login

}

Within the template:

<br><iframe ng-src="{{currentProjectUrl}}"> <!--content--> </iframe><br>

By employing trustAsResourceUrl, you can safely set the URL within the src attribute.

The above is the detailed content of How to Trust and Set iFrame Src Attribute from a Variable in AngularJS?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Previous article:How is HTML5 localStorage Isolated: Per Page or Domain? Next article:How to Set an iFrame\'s src Attribute from a Variable in AngularJS Using $sce Service?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!