michalv
michalv

Reputation: 375

Angular filter doesn't print white spaces

When I have such very easy Angular filter code:

{{ 'result: ' + array | printArray }}

with filter:

app.filter('printArray', [
  function() {
    return function(array) {
      if (!angular.isArray(array)) {
        return array;
      }

      var result = '';

      if (array.length > 0) {
        result = array[0];

        for (var i = 1; i < array.length; i++) {
          result += ', ' + array[i];
        }
      }
      return result;
    };
  }
]);

I would like to have naturally result: one, two, three, four, but the result is:

abc one,two,three,four

I knot it would be resolved by this obvious code:

{{ 'result: ' }}{{ array | printArray }}

but I would like to know why Angular works in weird way.

Plunker: http://plnkr.co/edit/QRtntKedYHKb5UnZD3w7

Upvotes: 0

Views: 266

Answers (2)

dhavalcengg
dhavalcengg

Reputation: 4713

Checkout this

  <body ng-controller="MainCtrl">
  {{ 'result: ' + ( array | printArray ) }}

  </body>

Upvotes: 3

Nitsan Baleli
Nitsan Baleli

Reputation: 5458

in this example i used Array.join() function,
and moved the string to outside of the curly braces:

result: {{ array | printArray }}

http://plnkr.co/edit/CRKChEtF5HafdRPV8eW4?p=preview

Upvotes: 3

Related Questions