首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果angularjs的src不起作用

问如果angularjs的src不起作用
EN

Stack Overflow用户
提问于 2018-05-19 04:26:42
回答 1查看 1.7K关注 0票数 1

我正在尝试使用php从mysql检索youtube链接,并将其嵌入到带有angularjs的网页中。问题是,除了iframe之外,所有其他数据都显示在网页上。正如您在下面的“我的代码视图”中看到的那样,I已经在网页上显示了post.src。这个链接正是我所期望的。但是,当我尝试嵌入它并试图在iframe源中使用{{ post.src }}传递链接时,它不会显示任何内容,但是它会按照iframe宽度和高度的指示留出一些空间。

有什么想法吗?

代码语言:javascript
复制
<!-- HTML content -->
<body ng-app="myApp">
<div ng-controller="videoControl">
    <table>
      <tr ng-repeat="post in posts">
        <td>{{ post.postType }}</td>
        <td>{{ post.postTitle }}</td>
        <td>{{ post.postDescription }}</td>
        <td>{{ post.postDate }}</td>
        <td>{{ post.src }} </td>
        <td>
            <iframe width='560' height='315' ng-src='{{ post.src }}' frameborder='0' allow='autoplay; encrypted-media' allowfullscreen></iframe>
        </td>
      </tr>
    </table>
</div>







<!-- module -->
var app = angular.module("myApp", ["ngRoute"]);






<!-- controller -->
app.controller('videoControl', function($scope, $http) {
    $http.get("pages/db_section/videos.php")
    .then(function (response) {
        $scope.posts = response.data.records;
    });
});






<!-- videos.php -->
<?php

include 'db.php';

connection();

$sql = "SELECT * FROM feed WHERE post_type='video' ORDER BY time DESC";
$result = $conn->query($sql);

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}

    $source = str_replace("watch?v=","embed/",$rs["src"]);

    $outp .= '{"postType":"'  . $rs["post_type"] . '",';
    $outp .= '"postDate":"'   . $rs["time"]        . '",';
    $outp .= '"postTitle":"'   . $rs["post_title"]        . '",';
    $outp .= '"src":"'   . $source      . '",';
    $outp .= '"postDescription":"'. $rs["post_description"]     . '"}';
}
$outp ='{"records":['.$outp.']}';


connectionClose();

echo($outp);

?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-19 04:30:34

您需要使用一个过滤器,其中'post.src‘是iframe的URL,'trustAsResourceUrl’是过滤器,并且是定义的。

代码语言:javascript
复制
angular.module('myApp', [])
.filter('trustAsResourceUrl', ['$sce', function($sce) {
    return function(val) {
        return $sce.trustAsResourceUrl(val);
    };
}])

和HTML

代码语言:javascript
复制
 <iframe width='560' height='315' ng-src='{{ post.src | trustAsResourceUrl}}' frameborder='0' allow='autoplay; encrypted-media' allowfullscreen></iframe>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50421765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档