-1

Videogular Angular 视频播放器喜欢在这样的对象中引用视频:

{
    preload: "auto",
    sources: [
        {
            src: $sce.trustAsResourceUrl("http://static.videogular.com/assets/videos/videogular.mp4"),
            type: "video/mp4"
        }
    ],
    theme: "bower_components/videogular-themes-default/videogular.css"
}

$sce.trustAsResourceUrl运行 URLng-sanitize以防止恶意代码。

在我的控制器中,我在将视频上传到 Firebase 存储后制作了这个对象,该存储返回一个snapshot对象:

var videoObject = {
    preload: "auto",
    sources: [
        {
            src: "$sce.trustAsResourceUrl(" + $scope.snapshot.downloadURL + ")",
            type: "video/" + $scope.mediaFormat
        },
    ],
    theme: {
        url: "http://www.videogular.com/styles/themes/default/latest/videogular.css"
    }
};

结果是:

{
    preload: "auto",
    sources: [
        {
            src: "$sce.trustAsResourceUrl(http://static.videogular.com/assets/videos/videogular.mp4)",
            type: "video/mp4"
        }
    ],
    theme: "bower_components/videogular-themes-default/videogular.css"
}

这不起作用,因为src键的值是字符串,而我希望 a$sce.trustAsResourceUrl不是字符串。

如果我这样制作对象:

var videoObject = {
    preload: "auto",
    sources: [
        {src: $sce.trustAsResourceUrl +'("' + $scope.snapshot.downloadURL + '")', type: "video/" + $scope.mediaFormat}
    ],
    theme: {
        url: "http://www.videogular.com/styles/themes/default/latest/videogular.css"
    }
};

然后我得到:

{
    preload: "auto",
    sources: [
        {
            src: "function (b){return g(a,b)}(\"http://static.videogular.com/assets/videos/videogular.mp4)",
            type: "video/mp4"
        }
    ],
    theme: "bower_components/videogular-themes-default/videogular.css"
}

根据Mozilla的说法,“JavaScript 对象是键和值之间的映射。键是字符串(或符号),值可以是任何东西。” 如果值可以是任何东西,我如何制作一个部分是字符串而部分不是字符串的值?什么叫“部分是字符串,部分不是字符串”?字面意思?

4

2 回答 2

0

让我们关注给您带来麻烦的片段:

{
    src: "$sce.trustAsResourceUrl(" + $scope.snapshot.downloadURL + ")",
    type: "video/" + $scope.mediaFormat
},

在这里,您正在执行字符串连接

"$sce.trustAsResourceUrl(" + // type: String
$scope.snapshot.downloadURL +  // type: String
")"                            // type: String
// Result is of type: String

同样,在您的第二个示例中,您试图将一个函数与一个字符串连接起来,这将导致该函数被强制转换为一个字符串。如果您想要此行为的清晰示例,请运行以下代码段:

function hello () { return "hello" }
var result = hello + "world"
// ----------------^ this implicitly calls toString on the function
console.log("result: ", hello + "world")
console.log("result type: ", typeof result)

解决方案:调用函数,传入 url 作为参数:

{
    src: $sce.trustAsResourceUrl($scope.snapshot.downloadURL),
    type: "video/" + $scope.mediaFormat
},
于 2017-03-13T23:30:21.353 回答
0

我认为这样的事情应该有效,

function something() { return $sce.......; } source[0].src = something();

于 2017-03-13T23:28:34.410 回答