2014-12-07 5 views
0

я получаю следующее сообщение об ошибке:TypeError: Не удается прочитать свойство 'пост' неопределенной

TypeError: Cannot read property 'post' of undefined 
    at postName (http://127.0.0.1:9000/scripts/controllers/main.js:28:12) 
    at Scope.$scope.submit (http://127.0.0.1:9000/scripts/controllers/main.js:10:7) 
    at http://127.0.0.1:9000/bower_components/angular/angular.js:10348:21 
    at http://127.0.0.1:9000/bower_components/angular/angular.js:18333:17 
    at Scope.$eval (http://127.0.0.1:9000/bower_components/angular/angular.js:12175:28) 
    at Scope.$apply (http://127.0.0.1:9000/bower_components/angular/angular.js:12273:23) 
    at Scope.$delegate.__proto__.$apply (<anonymous>:855:30) 
    at HTMLFormElement.<anonymous> (http://127.0.0.1:9000/bower_components/angular/angular.js:18332:21) 
    at HTMLFormElement.jQuery.event.dispatch (http://127.0.0.1:9000/bower_components/jquery/dist/jquery.js:4641:9) 
    at HTMLFormElement.elemData.handle (http://127.0.0.1:9000/bower_components/jquery/dist/jquery.js:4309:46) angular.js:9563(anonymous function) angular.js:9563(anonymous function) angular.js:7004Scope.$apply angular.js:12275$delegate.__proto__.$apply VM1976:855(anonymous function) angular.js:18332jQuery.event.dispatch jquery.js:4641elemData.handle 

Мой main.js файл:

'использовать строгий';

angular.module('sayHiApp') 
    .controller('MainCtrl', function ($scope) { 

    // Accepts form input 
    $scope.submit = function() { 

     // POSTS data to webservice 
     postName($scope.input); 

     // GET data from webservice 
     var name = getName(); 

     // DEBUG: Construct greeting 
     $scope.greeting = 'Sup ' + name + ' !'; 

    }; 

    function postName ($scope, $http, dataToPost) { 

     $http.post('/name', dataToPost). 
     success(function(data) { 
     $scope.error = false; 
     $scope.data = data; 
     }). 
     error(function(data) { 
     $scope.error = true; 
     $scope.data = data; 
     }); 
    } 

    // GET name from webservice 
    function getName ($scope, $http) { 

     $http.get('/name'). 
     success(function(data) { 
     $scope.error = false; 
     $scope.data = data; 

     return data; 
     }). 
     error(function(data) { 
     $scope.error = true; 
     $scope.data = data; 

     return 'error name'; 
     }); 

    } 

    }); 

Я не уверен, что эта ошибка имеет в виду? Если он со ссылкой на «пост» метода на «$ HTTP», то я очень смущен .. Заранее спасибо за любую помощь :)

+4

Вы передаете параметр $ http функции вообще? – Mattigins

+1

Ahh man .. Да, это проблема. Отсосает к этому новое. Спасибо! :) – Tiwaz89

ответ

4

Это относится к «пост» метода на «$ HTTP», как вы предложили.

Вам нужно добавить $ http в качестве параметра в функции контроллера, поэтому угловое будет вводить его (так же, как вы делали с $ scope).

Я сделал еще одно изменение вашего кода, удалил параметры $ scope и $ http из внутренних функций, потому что они известны в функции из-за закрытия.

angular.module('sayHiApp') 
    .controller('MainCtrl', function ($scope, $http) { 

// Accepts form input 
$scope.submit = function() { 

    // POSTS data to webservice 
    postName($scope.input); 

    // GET data from webservice 
    var name = getName(); 

    // DEBUG: Construct greeting 
    $scope.greeting = 'Sup ' + name + ' !'; 

}; 

function postName (dataToPost) { 

    $http.post('/name', dataToPost). 
    success(function(data) { 
    $scope.error = false; 
    $scope.data = data; 
    }). 
    error(function(data) { 
    $scope.error = true; 
    $scope.data = data; 
    }); 
} 

// GET name from webservice 
function getName() { 

    $http.get('/name'). 
    success(function(data) { 
    $scope.error = false; 
    $scope.data = data; 

    return data; 
    }). 
    error(function(data) { 
    $scope.error = true; 
    $scope.data = data; 

    return 'error name'; 
    }); 

} 

});

1

функция postName не нужно иметь эти переменные, переданные ему $scope, $http ему нужны только dataToPost, остальные два вара уже доступны. Ваша функция должна просто смотреть, как этот

function postName (dataToPost) { 

    $http.post('/name', dataToPost). 
    success(function(data) { 
    $scope.error = false; 
    $scope.data = data; 
    }). 
    error(function(data) { 
    $scope.error = true; 
    $scope.data = data; 
    }); 
} 
Смежные вопросы