javascript newline character in AngularJS -


i want print out dynamic text div called sqloutput. want formatted newline characters. i've tried (obviously <br> , /\r\n). didn't work.

how can have formatted text using angular?

$scope.buildscripts = function () {     var maintable = "drop table [dbo].[" + $scope.tablename + "] <br>"         + "go <br>"         + "set ansi_nulls on <br>"         + "go <br>"         + "set quoted_identifier on <br>"         + "go <br>"         + "set ansi_padding on <br>"         + "go <br>"         + "create table [dbo].[" + $scope.tablename + "](";      $scope.sqloutput = maintable; } 

working example : http://plnkr.co/edit/wglhp7dzep9ih9yzntqy?p=preview

if want display in view html code variable have create filter. filter authorise interpreted html code. default feature disabled prevent security issues. (more reading her , her )

1) create filter :

// declare main module var myapp = angular.module('myapp', []);  angular.module('myapp').filter('unsafe', function ($sce) {    return function (val) {       if( (typeof val == 'string' || val instanceof string) ) {          return $sce.trustashtml(val);       }    }; });   myapp.controller('ctrl1', ['$scope',  function($scope) {     $scope.tablename = "userxxx" ;     $scope.buildscripts = function () {         var maintable = "drop table [dbo].[" + $scope.tablename + "] <br>"             + "go <br>"             + "set ansi_nulls on <br>"             + "go <br>"             + "set quoted_identifier on <br>"             + "go <br>"             + "set ansi_padding on <br>"             + "go <br>"             + "create table [dbo].[" + $scope.tablename + "](";          $scope.sqloutput = maintable;     }      $scope.buildscripts(); }]); 

2) use filter in view :

<span ng-bind-html="sqloutput  | unsafe "></span> 

Comments