gpt4 book ai didi

AngularJS 中的 javascript 换行符

转载 作者:行者123 更新时间:2023-12-03 07:51:20 26 4
gpt4 key购买 nike

我想将一些动态文本打印到我的 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

如果你想在 View 中显示变量中的一些 html 代码,你必须创建一个过滤器。该过滤器将授权解释的 html 代码。默认情况下,此功能处于禁用状态以防止出现安全问题。(更多阅读 herher )

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)在 View 中使用过滤器:

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

关于AngularJS 中的 javascript 换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com