1

Error: this.filters.forEach is not a function

.provider('searchFilter', function() {
  this.filters = {
    location : {
      default: 'Boston, MA',
      value: ''
    },
    radius : {
      default: '1',
      value: ''
    }
  }
  this.$get = function() {
      var filters = this.returnFilters();
      return {
          returnFilters: function() {
            return filters;
          }
      }
  };
  this.setLocation = function(location) {
      this.filters.location.default = location;
  };
  this.setRadius = function(radius) {
      this.filters.radius.default = radius;
  };
  this.returnFilters = function() {
    this.filters.forEach(function (filter) {
      filter.value = (filter.value=='')?filter.default:filter.value;
    });
    return this.filters;
  };
});

为什么我不能forEach.provider服务中使用?

4

1 回答 1

0

filters是一个对象,它没有“forEach”方法。但是,您可以使用for(.. in ..)or Object.keys(..).forEach(甚至angular.each我认为)

于 2013-05-29T20:38:27.437 回答