我正在尝试构建 Angularjs 应用程序,但我在使用 Controller 时遇到了问题。
'use strict';
/* Controllers */
angular.module('myApp.controllers', []).
controller('AppCtrl', function ($scope, $http) {
}).
controller('indexCTRL', function ($scope) {
$http.get('/api/frettir').
success(function(data, status, headers, config) {
$scope.posts = data;
});
});
但是当我运行它时出现一个错误,提示未定义 $http。我可以改进什么?
最佳答案
您需要在您使用的 Controller 中注入(inject)$http
服务。我喜欢这个非常精确 inline syntax如果您的代码通过缩小器,这将避免出现问题。
controller('indexCTRL', ['$scope', '$http',
function($scope, $http) {
//...your code
}
])
关于javascript - 变量 $http 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30198596/