我有一个简单的angularjs应用程序,我想发出http请求,但我真的不知道为什么它不工作.这是我的控制器:
(function () {
'use strict';
/**
* @ngdoc function
* @name app.controller:HomeCtrl
* @description
* # HomeCtrl
* Controller of the app
*/
angular.module('BlurAdmin.pages.dashboard')
.controller('HomeCtrl', ['$scope', '$rootScope', '$stateParams', '$http', '$location', HomeCtrl ]);
function HomeCtrl($scope, $http, $location) {
$scope.samples = [['a', 'b', 'c'], ['d', 'e', 'f']];
console.log($http);
console.log($scope);
console.log($location);
console.log(window.location.hostname);
$http.get(
'http://localhost' + ':7000/samples',
{}
).then(function(err, data){
if (err){
console.log(err);
}
console.log(data);
},
function(err, data){
});
var varApplist = this;
}
})();
Run Code Online (Sandbox Code Playgroud)
我的家庭主持人:
<div id="page-wrapper" ng-controller="HomeCtrl">
......
<div>
Run Code Online (Sandbox Code Playgroud)
在我的index.html中
当我尝试运行时,我遇到了这个错误:
TypeError: $http.get is not a function
at new HomeCtrl (http://localhost:4001/app/pages/dashboard/home/homeCtrl.js:21:11)
at invoke (http://localhost:4001/bower_components/angular/angular.js:4570:17)
at Object.instantiate (http://localhost:4001/bower_components/angular/angular.js:4578:27)
at http://localhost:4001/bower_components/angular/angular.js:9460:28
at http://localhost:4001/bower_components/angular-ui-router/release/angular-ui-router.js:4081:28
at invokeLinkFn (http://localhost:4001/bower_components/angular/angular.js:9099:9)
at nodeLinkFn (http://localhost:4001/bower_components/angular/angular.js:8586:11)
at compositeLinkFn (http://localhost:4001/bower_components/angular/angular.js:7975:13)
at publicLinkFn (http://localhost:4001/bower_components/angular/angular.js:7855:30)
at updateView (http://localhost:4001/bower_components/angular-ui-router/release/angular-ui-router.js:4021:23) <div ui-view="" class="ng-scope">
Run Code Online (Sandbox Code Playgroud)
当我记录$ location和$ http时,我有空对象.
.controller('HomeCtrl', ['$scope', '$rootScope', '$stateParams', '$http', '$location', HomeCtrl ]);
function HomeCtrl($scope, $http, $location) {
Run Code Online (Sandbox Code Playgroud)
除此之外$scope,函数的实际参数根本不匹配在数组开头声明的依赖项.
帮自己一个忙,并停止使用这个丑陋且容易出错的数组表示法.使用ng-annotate可以使代码自动缩小.
另请注意,传递给的回调then()将使用单个参数调用.所以data你在那里的论点毫无用处.