$ sce.trustAsHtml返回错误:类型错误:html.indexOf不是一个函数
问题描述:
代码示例:
<p ng-bind-html="htmlcode | unsafe"></p>
var myApp = angular.module('myApp', ["ngSanitize"]);
myApp.controller('helloController', ["$scope", "$sce", function ($scope, $sce) {
$scope.htmlcode="text <b>ng-bind-html</b>";
}
}])
.filter('unsafe', function($sce) { return $sce.trustAsHtml; });
TypeError: html.indexOf is not a function
我有 “santize.js”。
答
您有一个支架错字。
myApp.controller('helloController', ["$scope", "$sce", function ($scope, $sce) {
$scope.htmlcode="text <b>ng-bind-html</b>";
}])
.filter('unsafe', function($sce) { return $sce.trustAsHtml; });
删除额外的支架,应该工作。
答
您不包括与indexOf
NOR或var html
的部分。但无论如何,这是一个普遍的问题。 TrustAsHtml返回一个Object而不是一个字符串。所以你不能使用indexOf
就可以了。你必须使用第二个函数来获取字符串。
var htmlText = "<b>Hello</b>";
var value = $sce.trustAsHtml(htmlText);
var yourString = $sce.getTrustedHtml(value);
var index = yourString.indexOf("e");
你有另一个代码工作正常的问题。 http://jsfiddle.net/michelem09/yupds5ky/ –