ng-bind-html在ui-view中不起作用

时间:2017-03-20 12:58:46

标签: angularjs angular-ui-router ng-bind-html

我正在开发角度为1.4和ui.router 0.2.8的角应用程序。我正在使用ng-bind-html在表单验证时打印错误消息。但它无法正常工作。 在我的控制器中

$scope.nameError = $sce.trustAsHtml("<p class='form-error'>Please enter your full name</p>"); 

在我的html页面中,

<span ng-bind-html="nameError"></span>

显示没有错误。如何解决此问题并提前感谢?

2 个答案:

答案 0 :(得分:1)

你有&#39; ngSanitize&#39;在您的申请中设置?像:

   var app = angular.module('app', ['ngSanitize']);
    app.controller('myController', function($scope, $sce) {
    $scope.nameError = $sce.trustAsHtml("<p class='form-error'>
Please enter your full name</p>"); 
    });

您可以尝试此参考:

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular-sanitize.js"></script>

答案 1 :(得分:0)

你可以创建像@ Und3rTow这样的过滤器,也可以创建一个转换它的函数

$scope.trustFunc = function(nameError){
     return $sce.trustAsHtml("<p class='form-error'>Please enter your full name</p>");
}

<span ng-bind-html="trustFunc(nameError)"></span>

angular.module("app",[])
.controller("ctrl",function($scope,$sce){
  
$scope.trustFunc = function(nameError){
 return $sce.trustAsHtml("<p class='form-error'>Please enter your full name</p>");
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <span ng-bind-html="trustFunc(nameError)"></span>
</div>