3

Hi I know this is an easy question how can you insert a new line in a directive template? I have a long template. and it is hard for me to scan through horizontally. I want to have it in a new line. However angular doesn't want.

app.directive('broadcasted', function(){
return{
    restrict: 'EAC',
    // NEW LINE  THE TEMPLATE NOT JUST IN A SINGLE LINE
    template: '<div class="alert alert-success col-md-6" ng-repeat="x in bcards"><strong class="broadcast-text" ><% x.q_number %> - <% x.teller_id %></strong></div>',
    link: function($scope){
    }
};
});     
Divya MV
  • 2,021
  • 3
  • 31
  • 55
drake24
  • 525
  • 1
  • 11
  • 32

4 Answers4

3

How about this:

app.directive('broadcasted', function(){
return{
    restrict: 'EAC',
    // NEW LINE  THE TEMPLATE NOT JUST IN A SINGLE LINE
    template: '<div class="alert alert-success col-md-6" ng-repeat="x in bcards">' + 
              '<strong class="broadcast-text" >' + 
              '<% x.q_number %> - <% x.teller_id %></strong></div>',
    link: function($scope){
    }
};

There's also another way described here: Creating multiline strings in JavaScript

Community
  • 1
  • 1
pablochan
  • 5,625
  • 27
  • 42
3

The best way to do it is to put template in seperate HTML file and use templateUrl

app.directive('broadcasted', function(){
return{
    restrict: 'EAC',
    // NEW LINE  THE TEMPLATE NOT JUST IN A SINGLE LINE
    templateUrl: 'mytempalte.html',
    link: function($scope){
    }
};

mytemplate.html

<div class="alert alert-success col-md-6" ng-repeat="x in bcards">
   <strong class="broadcast-text" >
              <% x.q_number %> - <% x.teller_id %>
   </strong>
</div>
squiroid
  • 13,809
  • 6
  • 47
  • 67
  • +1 while this is not exactly what the question asked, this approach is cleaner than concatenating strings in JS source code. – Igor Raush Jul 02 '15 at 07:17
  • @igorraush thanks :) . IT's better to guide the OP in right direction. I know i can give answer in terms of string concat with '+' but it will not run for long if template is very long it's better to keep it in seperate HTML :) – squiroid Jul 02 '15 at 07:37
1

I believe you can use escape character \

app.directive('broadcasted', function(){
  return{
    restrict: 'EAC',
    template: '<div class="alert alert-success col-md-6" ng-repeat="x in bcards"> \
               <strong class="broadcast-text" ><% x.q_number %> - <% x.teller_id %></strong> \
               </div>',
    link: function($scope){
  }
};
geckob
  • 7,680
  • 5
  • 30
  • 39
0

Why to not concatenate with the + sign:

app.directive('broadcasted', function(){
return{
    restrict: 'EAC',
    // NEW LINE  THE TEMPLATE NOT JUST IN A SINGLE LINE
    template: '<div class="alert alert-success col-md-6" ng-repeat="x in bcards">' + 
                   '<strong class="broadcast-text" ><% x.q_number %> - <% x.teller_id %></strong>' + 
              '</div>',
    link: function($scope){
    }
};
Endre Simo
  • 11,330
  • 2
  • 40
  • 49