javascript $http.post undefined 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29061460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$http.post undefined is not a function
提问by Dirk Dunn
I am trying to implement a simple $http post with angular within this controller.
我正在尝试在此控制器中实现一个带有 angular 的简单 $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 = '';
};
}]);
This part
这部分
$http.post('/login',{ username: $scope.username, password: $scope.password });
is throwing this error
正在抛出这个错误
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)
here is the HTML for the ui-view.
这是 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>
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.
我已经检查了语法,一切看起来都正确,当我在控制器范围内控制台.log 时,$http 被识别为一个对象。但由于某种原因,我无法发出此发布请求,因为正在抛出错误。我正在使用 expressjs 来提供内容,而我当前的 '/login' 路由只是一个简单的 response.send('response sent!')。正如您在 HTML 中看到的,我使用的是 angular ui-router,其中我还为其设置了 .state,如果需要提供,请告诉我。
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.
我对此感到很沮丧,因为这显然是已知的功能,而且我无法在网上找到任何资源来帮助我解决确切的问题。我曾尝试在标题和其他在线解决方案中设置内容类型,但没有任何帮助。似乎它可能是愚蠢的,但我找不到它。
I would appreciate any input or help, thank you.
我将不胜感激任何输入或帮助,谢谢。
回答by dcodesmith
Dependency injection order is wrong
依赖注入顺序错误
app.controller('LoginCtrl', ['$scope', 'admin', '$http',function($scope, $http, admin) { ...
should be
应该
app.controller('LoginCtrl', ['$scope', '$http', 'admin', function($scope, $http, admin) { ...