gpt4 book ai didi

javascript - $sce.trustAsHtml 在字符串上失败

转载 作者:行者123 更新时间:2023-12-03 08:28:23 28 4
gpt4 key购买 nike

我正在尝试在字符串上运行 $sce.trustAsHtml 。它抛出 Error: $sce:itypeString Value is Required for SCE Trust Call即使我在返回字符串的函数上使用它。

这是一个PLUNKER以及导致错误的代码片段。

/*****
* Should display two images
* *****/
$scope.image = determineNeverSettleImage("engaging,innovative");
$scope.image = $sce.trustAsHtml($scope.image);

/************
* This function should:
* return 3 images if passed "engaging,innovating,partnering",
* return 2 images if passed "engaging,innovating",
* return 1 image if passed "engaging"
**************/
function determineNeverSettleImage(neverSettle) {
if (neverSettle.split(",").length > 1) {
neverSettleArray = neverSettle.split(",");
neverSettleStr = "";
for (i = 0; i < neverSettleArray.length; i++) {
if (neverSettleArray[i] == "engaging") {
neverSettleStr += '<img src="http://lorempixel.com/400/200/" width="56px" height="56px">';
}
if (neverSettleArray[i] == "innovating") {
neverSettleStr += '<img src="http://lorempixel.com/400/200/" width="56px" height="56px">';
}
if (neverSettleArray[i] == "partnering") {
neverSettleStr += '<img src="http://lorempixel.com/400/200/" width="56px" height="56px">';
}
if (neverSettleArray[i] == "synergy") {
neverSettleStr += '<img src="http://lorempixel.com/400/200/" width="56px" height="56px">';
}
}
$scope.test = neverSettleStr;
neverSettleStr = neverSettleStr + '';
return neverSettleStr;
} else {
switch (neverSettle) {
case "engaging":
neverSettle = '<img src="http://lorempixel.com/400/200/" width="56px" height="56px">';
return neverSettle;
case "innovating":
neverSettle = '<img src="http://lorempixel.com/400/200/" width="56px" height="56px">';
return neverSettle;
case "partnering":
neverSettle = '<img src="http://lorempixel.com/400/200/" width="56px" height="56px">';
return neverSettle;
case "synergy":
neverSettle = '<img src="http://lorempixel.com/400/200/" width="56px" height="56px">';
return neverSettle;
}
}
}

难道我的逻辑有问题?或者我是否以某种方式错误地使用了 $sce.trustAsHtml

最佳答案

我认为您遇到了问题,因为您为 $scope.image 分配了一个值,然后信任它。尝试:

$scope.image =  $sce.trustAsHtml(determineNeverSettleImage("engaging,innovative"));

顺便说一句,如果您想要提取资源,则应该使用 trustAsResourceUrl,然后您可以在 ngRepeat 中使用它。只是为了让您知道您有选择。

$scope.image = $sce.trustAsResourceUrl();

关于javascript - $sce.trustAsHtml 在字符串上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33444583/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com