0

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;
}
1

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

That worked great! Can you explain to me as someone new to Angular why I needed to do all this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.