jeevi
jeevi

Reputation: 31

can not read property success of undefined in angularjs v1

'use strict';
(function () {
    angular.module('loan-apply.directive.module')

        .directive('chitSlider', function () {
            return {
                restrict: 'E',
                templateUrl: 'src/modules/loan-apply/directives/chit-slider/chit-slider.html',
                link: chitFundCtrl
            }
        });

    function chitFundCtrl($scope, $http, element) {

        $http.get('src/temp/chit-fund-data.json')
            .success(function (data) {
                $scope.Details = data;
            })
            .error(function(data, status, headers, config) {
                // log error
            });

        setTimeout(function () {
            $(element).find('md-radio-group').slick({
                infinite: true,
                slidesToShow: 5,
                slidesToScroll: 5,
                arrows: true,
            });
        }, 0);


    }
})();

i'm getting TypeError: Cannot read property 'success' of undefined, i didnot understand what is the problem here , please help thank you.

Upvotes: 0

Views: 405

Answers (2)

Sravan
Sravan

Reputation: 18647

You missed the function return statement

return $http.get('src/temp/chit-fund-data.json')

So, code becomes:

function chitFundCtrl($scope, $http, element) {

    return $http.get('src/temp/chit-fund-data.json')
        .success(function (data) {
            $scope.Details = data;
        })
        .error(function (data, status, headers, config) {
            // log error
        });

    setTimeout(function () {
        $(element).find('md-radio-group').slick({
            infinite: true,
            slidesToShow: 5,
            slidesToScroll: 5,
            arrows: true,
        });
    }, 0);


}

ps: Now you can use then and catch instead of success

 return $http.get('src/temp/chit-fund-data.json')
    .then(function (data) {
       $scope.Details = data;
    })
    .catch(function (data, status, headers, config) {
      // log error
    });

Upvotes: 0

Padmapriya Vishnuvardhan
Padmapriya Vishnuvardhan

Reputation: 2166

I could see your code does not inject "$http".

Try it out like this

    angular.module('loan-apply.directive.module')
    .directive('chitSlider', ['$http', function($http){
    return {
                restrict: 'E',
                templateUrl: 'src/modules/loan-apply/directives/chit-slider/chit-slider.html',
                link: chitFundCtrl
            }
        });

function chitFundCtrl($scope, element) {

    $http.get('src/temp/chit-fund-data.json')
        .success(function (data) {
            $scope.Details = data;
        })
        .error(function(data, status, headers, config) {
            // log error
        });

    setTimeout(function () {
        $(element).find('md-radio-group').slick({
            infinite: true,
            slidesToShow: 5,
            slidesToScroll: 5,
            arrows: true,
        });
    }, 0);


}
})();

Upvotes: 1

Related Questions