2016-04-28 80 views
0

我試圖使用$sce解析字符串到html,但它不起作用。 我對angular controller功能:AngularJS將字符串解析爲html

function renderHtml(string) { 
    return $sce.trustAsHtml(string); 
} 

HTML:變量newfield.showConditions = gender <br /> vk

<div style="width:100px; word-wrap: break-word" ng-bind-html="ctrl.renderHtml(ctrl.newfield.showConditions)"></div> 

價值是什麼,我做錯了什麼?

+0

如果你只需要換行,你應該能夠\的方式 – Aides

+0

什麼是錯誤r \ N',而不是''
進入'? –

+0

你是否注入依賴? –

回答

0

我爲您的場景創建了一個JSBin,它工作正常。我已經做了被曝光的控制器的方法,改變使用保留字stringstr,並添加$sce作爲依賴的變化:

$scope.renderHtml = renderHtml; 

function renderHtml(str) { 
    return $sce.trustAsHtml(str); 
} 
+0

它不起作用 –

+0

你有沒有看到JSBin的鏈接? –

0

試試這個,包括ng-sanitize

function renderHtml($sce,string) { 
    return $sce.trustAsHtml(string); 
} 
+0

不幸的是,不起作用 –

0

嘗試。將ngSanitize depenednecy添加到應用程序。

angular.module('app', ['ngSanitize']) 

並且還添加ngSanitize js文件。

angular.module('app', ['ngSanitize']) 
 

 
.controller('ctrl',function($scope, $sce) { 
 
    $scope.html =$sce.trustAsHtml(
 
    ' this is <br> a <em>test</em>'); 
 
    
 
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular-sanitize.min.js"></script> 
 
<div ng-app="app"> 
 
    <div ng-controller="ctrl"> 
 
    <p ng-bind-html="html" ></p> 
 
    </div> 
 
</div>