0

I am developing an angular app with angular 1.4 and ui.router 0.2.8. I am using ng-bind-html to print error messages while form validation.But it is not working. In my controller

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

In my html page ,inside

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

Showing no errors.How can I solve this issue and thanks in advance?

2 Answers 2

1

Do you have 'ngSanitize' set up in your application? Like:

   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>"); 
    });

You can try this reference:

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular-sanitize.js"></script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all answers.@Und3rTow- Its working. But where is your answer to upvote
0

either you can create a filter like @Und3rTow mentioned or you can create a function to convert it

$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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.