收到此错误:
angular.min.js:122 TypeError:$ http.get(…)。success在Object.invoke(angular.min)的new(app.js:12)的getUserInfo(app.js:7)处不是函数.js:43)在Q.instance(angular.min.js:93)在p(angular.min.js:68)在g(angular.min.js:60)在g(angular.min.js:61) )在g(angular.min.js:61)在angular.min.js:60在angular.min.js:21
这是我的代码:
var gitHub = angular.module('gitHub', []); gitHub.controller('mainController', ['$scope', '$http', function($scope, $http) { var $scope.user = ''; function getUserInfo($scope, $http){ $http.get('https://api.github.com/users') .success(function (result) { $scope.user = result; console.log(result); }); }; getUserInfo($scope, $http); }]);
这是HTML
<!DOCTYPE html> <html ng-app="gitHub"> <head> <title>Github Users Directory</title> <script src="angular.min.js"></script> <script src="app.js"></script> </head> <body> <div ng-controller="mainController"> <div> <h1>GitHub Users</h1> Who do you want to search for?<input type="text" name="FindHim" ng-model="queryName" /> <button ng-click="getUserInfo()">Search</button> </div> <div> {{ user }} </div> </div> </body> </html>
该.success和.error方法已过时,已经从AngularJS1.6去除。请改用标准.then方法。
.success
.error
.then
$http.get('https://api.github.com/users') .then(function (response) { var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; $scope.user = data; console.log(data); });
弃用通知 在$http传统方法的承诺.success,并.error已被弃用,并将在V1.6.0被删除。请改用标准.then方法。 — AngularJS(v1.5)$ http服务API参考- 弃用声明。
在$http传统方法的承诺.success,并.error已被弃用,并将在V1.6.0被删除。请改用标准.then方法。
$http
— AngularJS(v1.5)$ http服务API参考- 弃用声明。