How to set an iframe src attribute from a variable in AngularJS
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>
How to change iframe src use angularjs
You need also $sce.trustAsResourceUrl
or it won't open the website inside the iframe:
JSFiddle
HTML:
<div ng-app="myApp" ng-controller="dummy">
<button ng-click="changeIt()">Change it</button>
<iframe ng-src="{{url}}" width="300" height="600"></iframe>
</div>
JS:
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');
}
}]);
How to set variable ng-src within one iframe using Angular Js
You can have a function defined and pass the url to it,
JS:
$scope.trustSrc = function(src) {
return $sce.trustAsResourceUrl(src);
}
HTML:
<md-button ng-click="selectMuppet(it)" ng-class="{'selected' : it === selected }">
<img ng-src="{{trustSrc(it.link)}}" class="face" alt="Sample Image">
{{it.sitename}}
</md-button>
Working APP
Angular 10: Set IFrame URL Input with Variable
Yes, it is still non-secure to put whatever into url same as in Angular.js. Just bypass it - it will be still non-secure, but you will take responsibility instead of angular developers.
constructor(public sanitizer: DomSanitizer) { }
ngOnInit() {
this.urlSafe= this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
}
write as [src]="urlSafe"
https://angular.io/api/platform-browser/DomSanitizer
ng-src attribute in an iframe doesn't accept expressions ? using Angular.js
As far as I know, this don't work because of security matters to previne xss attacks. But you can create a filter to trust the url like:
angular.module('filters', []).filter('trustThisUrl', ['$sce', function($sce) {
return function(val) {
return $sce.trustAsResourceUrl(val);
};
}]);
And then call it as filter of the url in src
<iframe ng-src="{{ anUrlFromYourController | trustThisUrl}}"></iframe>
angularjs ng-src within iframe
iframe and ng-src have been discussed on SO before with a solution using $sce (to trust the URL for ng-src='URL')
link: AngularJS ng-src inside of iframe
link: Unable to load url into iframe via AngularJS controller
link: How to set an iframe src attribute from a variable in AngularJS
GL!
AngularJS ng-src inside of iframe
You can use a filter instead:
HTML:
<iframe src="{{yourURL | trustAsResourceUrl}}"></iframe>
where 'yourURL' is the URL of the iframe and 'trustAsResourceUrl' is the filter and is defined as in some module(like eg. filters-module) as:
JS:
angular.module('filters-module', [])
.filter('trustAsResourceUrl', ['$sce', function($sce) {
return function(val) {
return $sce.trustAsResourceUrl(val);
};
}])
And you can use this filter in all the iframes and other embedded items in your application.
This filter will take care of all the urls that you need to trust just by adding the filter.
Related Topics
Javascript Add Class Active When Click a Link After Load Another Page
How to I Retrieve Last Part of Url and Use It in HTML
Focus Next Input Once Reaching Maxlength Value
How to Pass Checkbox State to Onclick Function in React
How to Know If Browser Tab Is Already Open Using JavaScript
Call Ajax or Submit the Button When Checkbox Is Checked
The Onclick Is Not Working on First Click
415 (Unsupported Media Type) With Rest Post Request
How to Check If Value Is in Array With Angularjs
Remove a Specific String from Url and Redirect
Syntaxerror: Cannot Use Import Statement Outside a Module
Laravel Pdf File Download from Ajax Request (Laravel 5)
Does Jest Swallow Console.Log Statements How to Change This
How to Remove a Parameter from a Url
How to Escape an Ampersand in a JavaScript String So That the Page Will Validate Strict
Deselect All Options in Multiple Select With 1 Option
Bootstrap Datepicker Appearing At Incorrect Location in a Modal