не может прочитать свойство успеха undefined в angularjs v1 - PullRequest
0 голосов
/ 08 мая 2018
'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);


    }
})();

Я получаю TypeError: Невозможно прочитать свойство 'success' из undefined, я не понимаю, в чем здесь проблема, пожалуйста, помогите, спасибо.

Ответы [ 2 ]

0 голосов
/ 08 мая 2018

Я вижу, что ваш код не вводит "$ http".

Попробуйте вот так

    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);


}
})();
0 голосов
/ 08 мая 2018

Вы пропустили оператор function return

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

Итак, код становится:

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);


}

пс: теперь вы можете использовать then и catch вместо success

 return $http.get('src/temp/chit-fund-data.json')
    .then(function (data) {
       $scope.Details = data;
    })
    .catch(function (data, status, headers, config) {
      // log error
    });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...