Angular JS 使用 trustasHtml 将字符串渲染为 html 显示错误
Angular JS rendering a string as an html with trustasHtml shows error
我有一个遗留应用程序,它有一个 ui 字段显示自定义 text.The 自定义文本现在也需要呈现链接,所以我更改了文本呈现以将其绑定为 html 这样,如果 html 中有任何锚标记,它们将在 ui 中呈现为链接。但出于某种原因,它对我不起作用。
$scope.setActiveTab = function(newTab) {
$scope.activeTab = newTab;
$scope.activeTexts = $sce.trustAsHtml($scope.tabMap[newTab]);
}
<div class="row" ng-repeat="activeText in activeTexts">
<div class="col--3"style="word-wrap : break-word"
ng-bind-html="activeText.customText">
</div>
</div>
当使用上面的代码时,对于一些 reason.I 是 angularJs 的新手,我在控制台中收到以下错误,我通读了文档但无法找到原因。
angular.js:11592 Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html
事实证明,$sce.trustAsHtml() 仅适用于字符串。我试图将它应用于一个数据数组,当函数期望一个字符串 argument.I 能够通过迭代数据并对 array.Below 代码中的每个对象应用清理来解决这个问题时,代码修复了它我:
$scope.setActiveTab = function(newTab) {
$scope.activeTab = newTab;
for (var i=0; i < $scope.activeTexts.length; i++) {
$scope.activeTexts[i].customText = $sce.trustAsHtml($scope.activeTexts[i].customText);
}
}
我有一个遗留应用程序,它有一个 ui 字段显示自定义 text.The 自定义文本现在也需要呈现链接,所以我更改了文本呈现以将其绑定为 html 这样,如果 html 中有任何锚标记,它们将在 ui 中呈现为链接。但出于某种原因,它对我不起作用。
$scope.setActiveTab = function(newTab) {
$scope.activeTab = newTab;
$scope.activeTexts = $sce.trustAsHtml($scope.tabMap[newTab]);
}
<div class="row" ng-repeat="activeText in activeTexts">
<div class="col--3"style="word-wrap : break-word"
ng-bind-html="activeText.customText">
</div>
</div>
当使用上面的代码时,对于一些 reason.I 是 angularJs 的新手,我在控制台中收到以下错误,我通读了文档但无法找到原因。
angular.js:11592 Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html
事实证明,$sce.trustAsHtml() 仅适用于字符串。我试图将它应用于一个数据数组,当函数期望一个字符串 argument.I 能够通过迭代数据并对 array.Below 代码中的每个对象应用清理来解决这个问题时,代码修复了它我:
$scope.setActiveTab = function(newTab) {
$scope.activeTab = newTab;
for (var i=0; i < $scope.activeTexts.length; i++) {
$scope.activeTexts[i].customText = $sce.trustAsHtml($scope.activeTexts[i].customText);
}
}