Search code examples
javascriptangularjsangular-filters

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


Solution

  • Checkout this

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