Angularsjs无法找到服务功能

时间:2017-10-14 18:06:12

标签: angularjs

我的filterProducts函数调用findIntersection,但是我得到一个错误,findIntersection是未定义的。

angular.module('BrandService', [])
.service('BrandService', function ($filter, DataService) {
    var productDb;
    var products;

    return {
        filterProducts(brands, priceRange) {
            var filteredProducts = [];
            var brandProducts = [];
            var priceProducts = [];
            var productsArray = [];

            var brandChecked = false;
            var priceChecked = false;

            angular.forEach(brands, function (brand) {
                if (brand.checked) {
                    brandChecked = true;

                    angular.extend(brandProducts,
                        $filter('filter')(productDb, { 'brand': brand.name }));
                }

                if (brandChecked) {
                    productsArray.push(brandProducts);
                    console.log('brandProducts = ', brandProducts)
                }
            });

            angular.forEach(priceRange, function (price) {
                if (price.checked) {
                    priceChecked = true;

                    let filteredProductDb = productDb.filter((prod) => {
                        return (prod.price >= price.low && prod.price <= price.high);
                    });
                    angular.extend(priceProducts, filteredProductDb);    
                }
            });

            if (priceChecked) {
                productsArray.push(priceProducts);
                // console.log('priceProducts = ', priceProducts)
            }

            if (!brandChecked && !priceChecked) {
                filteredProducts = products;
            } else {
                if (productsArray.length > 1) {
                    filteredProducts = findIntersection(productsArray);
                } else {
                    filteredProducts = productsArray[0];
                }
            }

            return filteredProducts;
        },

        findIntersection(productsArray) {
            console.log('findIntersection called')
            var filteredProducts = [];
            var filteredSet = new Set();

            for(var i=0; i < productsArray.length - 1; i++) {
                var products1 = productsArray[i];
                var products2 = productsArray[i+1];

                angular.forEach(products1, function(product1) {
                    angular.forEach(products2, function(product2) {
                        if(product1._id == product2._id) {
                            filteredSet.add(product1);
                        }
                    });
                });
            }

            filteredProducts = Array.from(filteredSet);
            return filteredProducts;
        }            
    }        
})

我的filterProducts函数调用findIntersection,但是我得到一个错误,findIntersection是未定义的。

我的filterProducts函数调用findIntersection,但是我得到一个错误,findIntersection是未定义的。

2 个答案:

答案 0 :(得分:1)

您正在返回包含属性的javascript对象。您没有定义全局函数。

您需要存储之前返回的服务:

var service = { findProducts: ... , findIntersection: ... };
return service; 

而不是致电findIntersection,请致电service.findIntersection

答案 1 :(得分:0)

您必须引用本地对象。

只需更改此内容:filteredProducts = findIntersection(productsArray);

filteredProducts = this.findIntersection(productsArray);