TypeError:无法读取属性' post'未定义的

时间:2014-12-07 06:23:03

标签: javascript angularjs

我收到以下错误:

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文件:

'use strict';

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'上的'post'方法,那么我很困惑..提前感谢任何帮助:)

2 个答案:

答案 0 :(得分:5)

这是指&#39; post&#39;关于&#39; $ http&#39;的方法正如你的建议。

你需要在控制器函数中添加$ http作为参数,所以angular会注入它(正如你对$ 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 :(得分: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;
  });
}