【问题标题】:Angular js - TypeError: Cannot read property 'get' of undefinedAngular js - TypeError:无法读取未定义的属性'get'
【发布时间】:2018-03-15 09:34:55
【问题描述】:

我有一个平均堆栈应用程序,在单击按钮时,将调用翻译函数。单击按钮时,它进入此函数,但无法执行引发错误的http.get 部分:

TypeError: 无法读取未定义的属性“get”

以下是该页面的 Angular 代码。

(function() {
    var app = angular.module('dashboard', []);
    app.config(['$stateProvider', function($stateProvider) {
        $stateProvider.state('secured.dashboard', {
            url: '/dashboard',
            controller: 'searchController',
            templateUrl: '/views/dashboard.html'
        });
    }]);

    app.controller('searchController', ['$scope', 'AuthService', '$state', '$http', function($scope, AuthService, user, $state, $http) {
        console.log($scope.user)
        // AuthService.setUser(user);
        console.log("In search controller");
        $scope.logout = function() {
            AuthService.logout().then(function() {
                $scope.user = null;
                $state.go('unsecured');
            })
        }

        $scope.data = {};

        $scope.translate = function() {
            console.log("Entered")
            console.log($scope.item);
            $scope.show_results = true;

            $http.get('/mosesTranslateSmall', { params: { name: $scope.item }, timeout: 600000 }).
            then(function(response) {
                console.log(response.data);
                $scope.data.moses_less = response.data;

            });

我查看了类似的问题,这些问题建议将 http 作为参数传递给控制器​​,但我正在这样做,但它仍然会引发错误。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    你的依赖顺序不对,把第二组参数中的user去掉

       app.controller('searchController', ['$scope', 'AuthService', '$state', '$http', function($scope, AuthService, $state, $http) {
    

    【讨论】:

    • 谢谢。它也有效,控制器和函数中的参数顺序是否应该相同?
    • 是的,首先它告诉我我只能在特定时间后标记它。我现在已经做到了。谢谢
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 2017-08-30
    • 1970-01-01
    • 2021-04-29
    • 2021-01-17
    • 2019-11-15
    相关资源
    最近更新 更多