2015-08-28 171 views
0

代码示例:

<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”。

+0

你有另一个代码工作正常的问题。 http://jsfiddle.net/michelem09/yupds5ky/ –

回答

0

您有一个支架错字。

myApp.controller('helloController', ["$scope", "$sce", function ($scope, $sce) { 

    $scope.htmlcode="text <b>ng-bind-html</b>"; 

}]) 
.filter('unsafe', function($sce) { return $sce.trustAsHtml; }); 

删除额外的支架,应该工作。

1

您不包括与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"); 
相关问题