1

我有一个正在构建的 AngularJS 应用程序。而且我对 Angular 有点陌生,但我一直在学习教程等。我已经到了想PUT通过<form>. 因此,我将几个 Angular 文档和一个 SO 帖子组合在一起:

所以,最后我有这个:

HTML

<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>

控制器

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);
                });
    };
}

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

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
4

1 回答 1

7

你需要注入它

function RegisterController($scope, $http)
于 2013-09-30T22:51:52.197 回答