Iframe has src but doesn't load body [duplicate] - html

I have a problem using ng-src inside of an iframe. I need to do this:
<div class="tab-content">
<ul class="nav nav-tabs" ng-repeat="document in issues.Document">
<div class="tab-pane pdf-height col-md-5 padding_0" id="{{document.name}}">
<iframe ng-src="http://192.168.223.110/cat/{{document.directory}}/{{document.name}}.{{document.type}}" height="100%" width="100%"></iframe>
</div>
</ul>
</div>
RESULT:
<iframe ng-src="http://192.168.223.110/cat/{{document.directory}}/{{document.name}}.{{document.type}}" height="100%" width="100%" src="http://192.168.223.110/cat/{{document.directory}}/{{document.name}}.{{document.type}}"></iframe>
I know that the problem is $sce, which is a protection from XSS, and that the link needs to be added to the whitelist... So it is working when I do this.
<ul class="nav nav-tabs" ng-repeat="document in issues.Document">
<div class="tab-pane pdf-height col-md-5 padding_0" id="{{document.name}}">
<iframe ng-src="{{someUrl}}" height="100%" width="100%"></iframe>
</div>
</ul>
And I define inside the controller:
$rootScope.someUrl = $sce.trustAsResourceUrl('http://192.168.223.110/cat/files/incoming/12345_3232ASD_pero.pdf');
But I can't do it like that because I'm looping with ng-repeat, so the link is generated dynamically. It need's to be readable from the database!

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.

Ok I found what the problem was..
Thank you for that filter it is working now :)
All I needed to do was to create ng-src link this:
<iframe ng-src="{{apiUrl+document.directory + '/' + document.name + '.'+ document.type | trustAsResourceUrl}}"
height="100%" width="100%">
</iframe>
Maybe this helps to someone! :)

As what Kop4lyf said
you need to add filter in js
//Create a filter for trust url
app.filter('trustAsResourceUrl', ['$sce', function($sce) {
return function(val) {
return $sce.trustAsResourceUrl(val);
};
}]);
and output ih html as
ng-src="{{x.title.rendered | trustAsResourceUrl}}"
Some other filter :
//Create a filter for trust html
app.filter('trustAsHtml', ['$sce', function($sce) {
return function(val) {
return $sce.trustAsHtml(val);
};
}]);

Related

Angularjs load json into a specific div

I'm new to AngularJS but I love the framework.
What I have right now, is a (stub) single page that loads json data.
JS
var merlinoApp = angular.module('merlino', []);
merlinoApp.controller('mainController', function ($scope, $http) {
...
$http.get('#Url.Action( "consoledatapull", "ConsoleElaborazioni")')
.then(function (res) {
$scope.jobs = res.data.jsonjobs;
$scope.clienti = res.data.jsonclienti;
$scope.console = res.data.jsonconsole;
});
...
});
HTML
<div ng-repeat="roll in jobs | orderBy:sortType:sortReverse | filter:searchJob | filter:searchCliente | filter:searchStato" class="console-row my-row">
...
<div class="console-cell-id console-cell console-cell-padding console-cell-no-border-sx">{{ roll.id }}</div>
...
<div ng-click="collapsed=!collapsed" ng-class="{'console-cell-esito-selected' : collapsed}" class="console-cell-esito console-cell console-cell-no-border-sx">SHORT DESC</div>
<div ng-show="collapsed" class="console-cell-esito-long console-cell console-cell-no-border-sx">{{ roll.esito }}</divng-show></div>
</div>
This populates ng-repeat, and the ng-click shows/hides the `ng-show div.
So far so good(?).
What Ì'm trying to achieve, is to load json data into
<div ng-show="collapsed" class="console-cell-esito-long...
if
<div ng-click="collapsed=!collapsed" ng-class="{'console-cell...
is clicked.
That is each div of ng-repeat, can be loaded with specific data:
<ul>
<li ng-repeat="logelem in jsonlog">
{{ logelem.log }}
</li>
</ul>
I thought about using a function:
<div ng-click="function(id)...
and then load json into a div identified by an id, so i used $index...
The result was, being able to load same data into all divs at once :/
Help would be appreciated.
My suggestion woudl be to add the information to the jobs elements itself.
So for example, the ng-click would become:
<div ng-click="loadData(id, roll)">CLICK ME</div>
and then the loadData would be something like:
$scope.loadData = function(id, roll){
// Do something
roll.result = result;
}
and then you can use the result from that object in the view like you would do in other places. You can then for example hide the object where you want the final result until the variable result is defined.
I think this will be the easiest solution.
Update from comments
Why not change the collapsed value in the method? Or you could use a $watch to listen to changes on the collapsed variable.

replace URL with href in ajax

Using following code to display text from another page
$.ajax({
url: 'topic/ginger.php',
success: function (data) {
data = $(data).find('div#display');
$('.res h2').html(data);
}
});
<a href="topic/ginger.php">
<div class="res" id="content" style="background-color:rgb(50, 190, 166)">
<h2></h2>
</div>
</a>
now i have two div with different href
<a href="topic/icecre.php">
<div class="res" id="content" style="background-color:rgb(224, 79, 95)">
<h2></h2>
</div>
</a>
<a href="topic/jelly.php">
<div class="res" id="content" style="background-color:rgb(92, 184, 92)">
<h2></h2>
</div>
</a>
As you see <a href="topic/ginger.php"> is url in ajax code, now i have two other div with different href so different url how do i sole this
$('.res').each(function(){
getAjax($(this).find("h2"), $(this).parent().attr('href'));
});
function getAjax(child, link)
{
$.ajax({
url: link,
success: function (data) {
data = $(data).find('div#display');
child.html(data);
}
});
}
Assuming all your divs are in the res class I would think this would do it, loops throught the res class and grabs the h2 inside it and the href of its parent and passes it to your ajax call to do the work. Havent tested it though.
EDITL just saw your newest edit, you cant have multiple items with the ID = "content" so get rid of them and give them each class ="res"

Load view after data is loaded

I have some trouble. I am using this plugin "angular-masonry" (it's on Github) to dynamically build the grid on the page. When the page loads I get this:
http://joxi.ru/YBQPVP3JTJCwfIgLgbc
Here is my code:
<div class="container" style="width:80%">
<h1 style="text-align: center; margin-bottom: 40px">
Category: {{category.text}}
</h1>
<div>(masonry='' load-images="false")
<div class="masonry-brick" ng-repeat="portal in category.claim_portals" style='width:50%;float:left'>
<div>
<h3>(style='margin-left:30px')
Portal: {{portal.text}}
</h3>
<div class="category-list" ng-repeat="claim in portal.portal_claim" style="margin-bottom:2px">
<div class="claim_sections">
<claimforlist claim="claim"></claimforlist>
</div>
</div>
</div>
</div>
</div>
</div>
But after resizing browser window, everything becomes normal and looks like this:
http://joxi.ru/iBQPVP3JTJCUfLnoqQQ
I think that view loads earlier than JSON data arrives.
Can anyone help and tell me how can I load view after the data has arrived? Or if you know another reason of such an issue, please reply.
Thanks in advance.
You can add a scope boolean variable with value set to false, and change the value to true on your http promise success.
Code sample:
function myController($scope, YourDataServer) {
$scope.dataLoadedSuccessfully = false;
yourDataServer
.query()
.$promise
.then(
function(result) {
$scope.dataLoaded = true; // set the value to true
});
}
HTML would look like:
<div id="loadingBar" ng-show="!dataLoadedSuccessfully">Loading data...</div>
<div id="dataWrapper" ng-show="dataLoadedSuccessfully">
<!-- data goes here -->
</div>

angular data isn't showing

I'm currently having problems outputing data in angularJS from a JSON.
I'm pulling JSON from SoundCloud to show song titles. Here is my app:
(function() {
// define the app
var app = angular.module('PartyPlayer', []);
// create the song lister controller
app.controller('SongListCtrl',
function ($scope, $http) {
$http.get("http://api.soundcloud.com/tracks/13158665.json?client_id=MY_CLIENT_ID").success(function(data) {
$scope.songs = data;
});
});
})();
and I'm trying to add the data here:
<div class="row" ng-controller="SongListCtrl as songList">
<div class="small-12 columns">
<div class="list">
<ul>
<li>{{songList.songs.title}}</li>
</ul>
</div>
But nothing shows up. When I console.log(data.title) I get the correct result, but not when I go to add it to my controller in the html. Anyone know the reason why this is happening? Any help would be gratefully appreciated! Thanks!
You have set a scope variable as songs and referring it in the html markup with songsList.songs
Your actual HTML Markup should be
<div class="row" ng-controller="SongListCtrl as songList">
<div class="small-12 columns">
<div class="list">
<ul>
<li>{{songs.title}}</li>
</ul>
</div>

angularjs template ng-view

Anyone can please tell me what im doing wrong here:
i just update the library, and seems that the code broke for some reason it's not doing anything.
angular library
html5 code
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<header id="header">
<h1 id="logo"></h1>
<div id="menu">
<a ng-click="setRoute('home')" class="btn">Home</a>
<a ng-click="setRoute('about')" class="btn">about</a>
<a ng-click="setRoute('experiments')" class="btn">Experiments</a>
</div>
<div class="color"></div>
<div class="clear"></div>
</header>
<!-- //top -->
<div class="shadow"></div>
<div id="container">
<div ng-view></div>
</div>
angular.js
angular.module('WebSite', []).
config(function($routeProvider) {
$routeProvider.
when('/about', {templateUrl:'folder/test.html', controller:AboutCtrl}).
when('/experiments', {templateUrl:'folder/test.html', controller:ExperimentsCtrl }).
when('/home', {templateUrl:'folder/test.html', controller:HomeCtrl }).
otherwise({redirectTo:'/home'});
});
function AboutCtrl($scope) {
$scope.title = 'About Page';
$scope.body = 'This is the about page body';
}
function ExperimentsCtrl($scope) {
$scope.title = 'Experiments Page';
$scope.body = 'This is the about experiments body';
}
function HomeCtrl($scope) {
$scope.title = 'Home Page';
$scope.body = 'This is the about home body';
}
First you need to use ng-app to setup the angular application with your module. Maybe you already did, but it is not part of the snippet you posted:
<html ng-app="WebSite">
Also, about the setRoute(), I don't know where you copy that from, but you don't need it. Just use the href with the hash, for example:
about
You need to check out the source from github at https://github.com/simpulton/angular-website-routes
There is no changeRoute method. After some dialog, I realized it was best to handle the anchors tags as naturally as possible.
Your menu element should look like this
<div id="menu">
Home
About
Experiments
</div>
Hope this helps!