iframe src不工作

ljo96ir5  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(279)

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

<!-- 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);

?>
nom7f22z

nom7f22z1#

您需要使用一个过滤器,其中'post.src'是iframe的url,'trustasresourceurl'是过滤器并已定义

angular.module('myApp', [])
.filter('trustAsResourceUrl', ['$sce', function($sce) {
    return function(val) {
        return $sce.trustAsResourceUrl(val);
    };
}])

和html

<iframe width='560' height='315' ng-src='{{ post.src | trustAsResourceUrl}}' frameborder='0' allow='autoplay; encrypted-media' allowfullscreen></iframe>

相关问题