0

I have the Angular controller below. When it is run, I get the error "TypeError: t.getDealers is not a function". This is caused by the minification of the javascript. However, I'm not sure how to fix this.

myApp.controller('DLOController', ['$scope', 'DLOFactory',
    function ($scope, DLOFactory) {

        $scope.init = function () {
            $scope.searchType = [{ id: 1, name: "Search by Postcode" }, { id: 2, name: "Search by State" }, { id: 3, name: "Search by Dealer" }];
            $scope.selectedSearchType = 1;
            $scope.selectedState = -1;
            $scope.postcode = "";
            $scope.dealerName = "";
            $scope.states = [];
            $scope.dealers = [];

            getStates(2);
            getDealers(2);

        }

        function getStates(categoryType) {
            DLOFactory.getStates(categoryType)
                .success(function (val) {
                    $scope.states = val;
                })
            .error(function (error) {
                // REVISIT: LOG ERROR HERE.
            });
        }

        function getDealers(categoryType) {
            DLOFactory.getDealers(categoryType)
                .success(function (val) {
                    $scope.dealers = val;
                    console.log($scope.dealers);
                })
            .error(function (error) {
                // REVISIT: LOG ERROR HERE.
            });
        }

    }
]);

Here is my factory:

myApp.factory('DLOFactory', ['$http', function ($http) {

    var stateList = [];
    var dealerList = [];

    return {
        getStates: function (categoryType) {
            return $http({
                method: 'GET',
                url: '/Addresses/GetStateList',
                params: { categoryType: categoryType }
            })
            .success(function (responseData) {
                stateList.push(responseData);
            });
        },
        getStateList: function () {
            return stateList;
        },
        setStateList: function (sl) {
            stateList = sl;
        }
    }

    return {
        getDealers: function (categoryType) {
            return $http({
                method: 'GET',
                url: '/Websites/GetDealers',
                params: { categoryType: categoryType }
            })
            .success(function (responseData) {
                dealerList.push(responseData);
            });
        },
        getDealerList: function () {
            return dealerList;
        },
        setDealerList: function (dl) {
            dealerList = dl;
        }

    }
}]);

The minified code looks like this:

myApp.controller("DLOController",["$scope","DLOFactory",function(n,t){function i(i){t.getStates(i).success(function(t){n.states=t}).error(function(){})}function r(i){t.getDealers(i).success(function(t){n.dealers=t;console.log(n.dealers)}).error(function(){})}n.init=function(){n.searchType=[{id:1,name:"Search by Postcode"},{id:2,name:"Search by State"},{id:3,name:"Search by Dealer"}];n.selectedSearchType=1;n.selectedState=-1;n.postcode="";n.dealerName="";n.states=[];n.dealers=[];i(2);r(2)}}]);

1 Answer 1

0

You have two returns on the factory so second return won't execute. So merge both returns & try.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried that, but I get compilation errors. Can you please show me how to merge them both, as I'm new to this. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.