[英]javascript newline character in AngularJS
I want to print out some dynamic text to my div called sqlOutput. 我想打印一些动态文本到我的div中,称为sqlOutput。 I want it to be formatted with newline characters.
我希望它使用换行符格式化。 I've tried (obviously
<br>
and /\\r\\n
). 我已经尝试过(显然
<br>
和/\\r\\n
)。 But it didn't work. 但这没有用。
How can I have a well formatted text using Angular? 如何使用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 工作示例: 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. 如果要在视图中显示某个变量的一些html代码,则必须创建一个过滤器。 this filter will authorise interpreted html code.
此过滤器将授权解释的html代码。 by default this feature is disabled to prevent security issues.
默认情况下,禁用此功能以防止安全问题。 (more reading her and her )
(更多阅读她和她的文章 )
1) create a filter : 1)创建一个过滤器:
// 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 : 2)在视图中使用过滤器:
<span ng-bind-html="sqlOutput | unsafe "></span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.