为什么我在这个控制器中得到“$ http未定义”?

时间:2013-09-30 22:50:33

标签: javascript html angularjs

我正在构建一个AngularJS应用程序。而且我对Angular有点新,但我一直在研究教程等。我已经达到了PUT通过<form>在服务器上<form id="register-form" ng-controller="RegisterController" ng-submit="registerUser()"> <label>Display Name</label> <input type="text" ng-model="user.displayname"></input> <label>Email Address</label> <input type="text" ng-model="user.username"></input> <label>Password</label> <input type="password" ng-model="user.password"></input> <a href="#/register" class="button primary gradient" onclick="$('#register-form').submit();">Register</a> </form> 数据的程度。所以,我把几个Angular文档和一个SO帖子组合在一起:

所以,最后我有这个:

HTML

function RegisterController($scope) {
    $scope.user = { };

    $scope.registerUser = function() {
        $http({
            method: 'PUT',
            url: '/user/register',
            data: $scope.user
        }).success(function(data) {
                    setAjaxMessage(data, true);
                }).error(function(data) {
                    setAjaxMessage(data, false);
                });
    };
}

控制器

Error: $http is not defined
RegisterController/$scope.registerUser@http://l.tpb.com/js/controllers.js:22
_functionCall/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:6541
ngEventDirectives[directiveName]</</</<@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:13256
Scope.prototype.$eval@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:8218
Scope.prototype.$apply@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:8298
ngEventDirectives[directiveName]</</<@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:13255
x.event.dispatch@http://l.tpb.com/js/jquery-1.10.2.min.js:5
x.event.add/v.handle@http://l.tpb.com/js/jquery-1.10.2.min.js:5
x.event.trigger@http://l.tpb.com/js/jquery-1.10.2.min.js:5
.trigger/<@http://l.tpb.com/js/jquery-1.10.2.min.js:5
.each@http://l.tpb.com/js/jquery-1.10.2.min.js:4
x.prototype.each@http://l.tpb.com/js/jquery-1.10.2.min.js:4
.trigger@http://l.tpb.com/js/jquery-1.10.2.min.js:5
x.fn[t]@http://l.tpb.com/js/jquery-1.10.2.min.js:6
onclick@http://l.tpb.com/#/register:1

但是,Firefox在执行该代码时会记录此错误:

{{1}}

1 个答案:

答案 0 :(得分:7)

你需要注射它

function RegisterController($scope, $http)