[英]javascript newline character in AngularJS
我想打印一些動態文本到我的div中,稱為sqlOutput。 我希望它使用換行符格式化。 我已經嘗試過(顯然<br>
和/\\r\\n
)。 但這沒有用。
如何使用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;
}
工作示例: http : //plnkr.co/edit/WgLHP7DzeP9iH9YzNTqY?p=preview
如果要在視圖中顯示某個變量的一些html代碼,則必須創建一個過濾器。 此過濾器將授權解釋的html代碼。 默認情況下,禁用此功能以防止安全問題。 (更多閱讀她和她的文章 )
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)在視圖中使用過濾器:
<span ng-bind-html="sqlOutput | unsafe "></span>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.