2015-08-28 281 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"); 
相關問題