forEach内部提供程序服务AngularJS

时间:2013-05-29 20:33:49

标签: javascript angularjs

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

1 个答案:

答案 0 :(得分:0)

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