$ http.post不确定是不是一个函数 [英] $http.post undefined is not a function
问题描述
我想实现一个简单的$ HTTP POST具有角这个控制器中。
I am trying to implement a simple $http post with angular within this controller.
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)
下面是用户界面视图的HTML。
here is the HTML for the ui-view.
<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是被认可为一个对象。我使用前pressjs服务的内容,我的'/登录当前路线只是一个简单的response.send('发送响应!')。正如你在我采用了棱角分明的UI路由器的HTML,其中我也有这方面的.STATE集,看是否需要提供请让我知道。
I have checked the syntax, everything looks correct, $http is being recognized as an object when I console.log it within the controllers scope.Yet for some reason I cannot make this post request because an error is being thrown. I am using expressjs to serve the content, and my current route for '/login' is just a simple response.send('response sent!'). As you can see in the HTML I am using angular ui-router, in which I also have a .state set for it, if this needs to be provided please let me know.
我一直受挫这一点,因为这是清楚知道的功能,我不能在网上找到帮助我确切的问题的任何资源。我已经试过了头,和其他在线解决方案设置内容类型,但什么也没有帮助。看起来这可能是一些愚蠢的,但我不能找到它。
I have been frustrated about this because this is clearly known function, and I cannot find any resources online to help me with the exact problem. I have tried setting the content type in the headers, and other online solutions but nothing has been helping. It seems like it may be something stupid, but I can't find it.
我想AP preciate任何输入或帮助,谢谢。
I would appreciate any input or help, thank you.
推荐答案
依赖注入顺序是错误的。
Dependency injection order is wrong
app.controller('LoginCtrl',['$scope','admin','$http',function($scope,$http,admin){ ...
应
app.controller('LoginCtrl',['$scope','$http', 'admin', function($scope,$http,admin){ ...
这篇关于$ http.post不确定是不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!