【问题标题】:$http.post undefined is not a function$http.post undefined 不是函数
【发布时间】:2015-03-15 14:00:41
【问题描述】:

我正在尝试在此控制器中实现一个简单的带有角度的 $http 帖子。

app.controller('LoginCtrl',['$scope','admin','$http',function($scope,$http,admin){
    $scope.isAdmin = admin.isAdmin;
    $scope.logout = admin.logout;

    $scope.login = function(){
        if(!$scope.username || !$scope.password ){ return; }
        $http.post('/login',{ username: $scope.username, password: $scope.password });
        $scope.username = '';
        $scope.password = '';
    };
}]);

这部分

$http.post('/login',{ username: $scope.username, password: $scope.password });

抛出这个错误

TypeError: undefined is not a function
    at l.$scope.login (http://localhost:3000/javascripts/angularApp.js:165:9)
    at hb.functionCall (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:198:426)
    at Cc.(anonymous function).compile.d.on.f (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:214:485)
    at l.$get.l.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:126:193)
    at l.$get.l.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:126:419)
    at HTMLFormElement.<anonymous> (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:215:36)
    at HTMLFormElement.c (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:32:363)

这里是 ui-view 的 HTML。

<script type="text/ng-template" id="/login.html">
    <button id="logout" class="btn btn-default" ng-click="logout()" ng-show="isAdmin()">Log Out</button>
    <div ng-controller="LoginCtrl">
      <form id="login" ng-submit="login()">
        <h2>The Login Page.</h2>
        <label>Username:</label>
        <input type="text" ng-model="username" placeholder="username"/>
        <label>Password:</label>
        <input type="password" ng-model="password" placeholder="password"/>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
    </div>
  </script>

我检查了语法,一切看起来都正确,当我在控制器范围内控制台.log 它时,$http 被识别为一个对象。但是由于某种原因,我无法发出这个 post 请求,因为抛出了一个错误。我正在使用 expressjs 来提供内容,而我当前的“/登录”路线只是一个简单的 response.send('response sent!')。正如您在 HTML 中看到的,我正在使用 angular ui-router,其中我还为其设置了 .state,如果需要提供,请告诉我。

我对此感到很沮丧,因为这是众所周知的功能,我无法在网上找到任何资源来帮助我解决确切的问题。我尝试在标题和其他在线解决方案中设置内容类型,但没有任何帮助。好像有点蠢,但我找不到。

如果有任何意见或帮助,我将不胜感激,谢谢。

【问题讨论】:

  • 您是否尝试将其作为参数转发给登录方法? $scope.login = function($http){
  • 谢谢 Davion,我刚刚这样做了,现在我得到一个不同的错误 TypeError: Cannot read property 'post' of undefined
  • 您没有提供正确的控制器初始化依赖项列表。 '$scope','admin','$http',function($scope,$http,admin 应该是 '$scope','admin','$http',function($scope, admin, $http)
  • 谢谢 Davion,解决了所有问题。

标签: javascript angularjs post routes


【解决方案1】:

依赖注入顺序错误

app.controller('LoginCtrl', ['$scope', 'admin', '$http',function($scope, $http, admin) { ...

应该是

app.controller('LoginCtrl', ['$scope', '$http', 'admin', function($scope, $http, admin) { ...

【讨论】:

  • 谢谢,成功了!不知道依赖注入需要先服务,感谢帮助像我这样的新手。
  • 不一定是第一个.. DI的顺序需要与传入控制器函数的参数相匹配。
猜你喜欢
  • 2021-10-16
  • 2016-08-02
  • 2020-10-12
  • 2015-10-29
  • 2015-06-05
  • 2019-10-16
  • 2015-06-12
  • 2015-10-16
  • 2013-05-17
相关资源
最近更新 更多