【问题标题】:iframe src from angularjs is not working来自 angularjs 的 iframe src 不起作用
【发布时间】:2018-10-29 12:16:49
【问题描述】:

我正在尝试使用 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);

?>

【问题讨论】:

    标签: php html mysql angularjs iframe


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-23
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      相关资源
      最近更新 更多