$http.post undefined 不是函数

$http.post undefined is not a function

我正在尝试在此控制器中使用 angular 实现一个简单的 $http post。

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>

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

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

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

依赖注入顺序错误

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

应该是

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