I want to print out some dynamic text to my div called sqlOutput. I want it to be formatted with newline characters. I've tried (obviously <br>
and /\\r\\n
). But it didn't work.
How can I have a well 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 you want to display in the view some html code from a variable you have to create a filter. this filter will authorise interpreted html code. by default this feature is disabled to prevent security issues. (more reading her and her )
1) create a filter :
// Declare the 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 the filter in the view :
<span ng-bind-html="sqlOutput | unsafe "></span>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.