I want to use an Angular markup to pull a URL from a database and insert it into the source of an iframe. This is with what I'm presently working:
<iframe ngSrc="{{post.url}}" width="300" height="80" frameborder="0" allowtransparency="true"></iframe>
When looking at Elements in Dev Tools, the URL string appears properly. However, the iframe does not render on the page.
EDIT: I found an example on Plunker that sort of works. The problem I'm having now is that it is assigning everything to the last url. Is there a way to differentiate them? This is my adaptation:
app.js
app.controller('MainController', ['$http', '$scope', '$sce', function($http, $scope, $sce) {
this.allPosts = [];
this.getAllPosts = () => {
$http({
url: "/posts", method: "get"
}).then(response => {
this.allPosts = response.data;
for (let post of this.allPosts) {
$scope.trustSrc = function(src) {
return $sce.trustAsResourceUrl(src)
}
$scope.track = {
src: post.url
}
}
console.log(this.allPosts);
}, ex => {
console.error(ex.data.err);
this.error = ex.statusText;
}).catch(err => this.error = "Server broke?");
};
html
<div class="all_posts" ng-repeat="post in ctrl.allPosts | filter: searchBox" ng-click="ctrl.getOne(post)">
<div class="one_post">
<div class="embedded_song">
<iframe ng-src="{{trustSrc(track.src)}}" width="300" height="80" type="text/html" frameborder="0" allowtransparency="true"></iframe>
{{ post.artist }} -
<a href="/one_post/">{{ post.songTitle }}</a>
<a href={{post.url}} target="blank">Listen here</a>
</div>
<!-- Submitted By -->
<div class="post_content">
submitted by: {{post.user.username}}<br>
<!-- Optional Text Box? -->
</div>
<!-- Tags -->
<div class="tag_area">
<div class="post_tags" ng-repeat="tag in post.tag">
<button class="post_tag">#{{tag}}</button>
</div>
</div>
</div>