来自angularjs的iframe src无效

时间:2018-05-19 04:26:43

标签: php html mysql angularjs iframe

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

任何想法???

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

?>

1 个答案:

答案 0 :(得分:2)

您需要使用过滤器,其中'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>