我想打印一些名为sqlOutput的div的动态文本。我想用换行符来格式化它。我试过了(显然是<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;
}
答案 0 :(得分:1)
工作示例:http://plnkr.co/edit/WgLHP7DzeP9iH9YzNTqY?p=preview
如果要在视图中显示变量中的一些html代码,则必须创建过滤器。此过滤器将授权解释的HTML代码。默认情况下,此功能已禁用以防止出现安全问题。 (更多阅读her和her)
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>