Angular未发布到URI

时间:2016-03-31 08:41:36

标签: javascript angularjs rest

当我在表单中输入数据时,我的角度代码没有发布到api,即使我的api已被证明有效并且需要curl CRUD请求。注册函数从我的partial调用,然后Register控制器使用用户服务和注册函数将模型发布到api

用户服务

(function () {
    'use strict';

    angular
        .module('app')
        .factory('UserService', UserService);

    UserService.$inject = ['$http'];
    function UserService($http) {
        var service = {};

        service.GetAll = GetAll;
        service.GetById = GetById;
        service.GetByUsername = GetByUsername;
        service.Create = Create;
        service.Update = Update;
        service.Delete = Delete;

        return service;

        function GetAll() {
            return $http.get('https://me.com/api/users/').then(handleSuccess, handleError('Error getting all users'));
        }

        function GetById(id) {
            return $http.get('https://me.com/api/users/' + id).then(handleSuccess, handleError('Error getting user by id'));
        }
...

    function Create(user) {
        return $http.post('https://me.com/api/users', user).then(handleSuccess, handleError('Error creating user'));
    }

.....

注册部分

<div ng-controller="RegisterController as vm">
<div class="col-md-6 col-md-offset-3">
    <h2>Register</h2>
    <form name="form" ng-submit="vm.register()" role="form">
        <div class="form-group" ng-class="{ 'has-error': form.fname.$dirty && form.fname.$error.required }">
            <label for="username">First name</label>
            <input type="text" name="fname" id="fname" class="form-control" ng-model="vm.user.fname" required />
            <span ng-show="form.fname.$dirty && form.fname.$error.required" class="help-block">First name is required</span>
        </div>
        <div class="form-group" ng-class="{ 'has-error': form.lname.$dirty && form.lname.$error.required }">
            <label for="username">Last name</label>
            <input type="text" name="lname" id="Text1" class="form-control" ng-model="vm.user.lname" required />
            <span ng-show="form.lname.$dirty && form.lname.$error.required" class="help-block">Last name is required</span>
        </div>
        <div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" class="form-control" ng-model="vm.user.username" required />
            <span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
        </div>
        <div class="form-group" ng-class="{ 'has-error': form.hashword.$dirty && form.hashword.$error.required }">
            <label for="hashword">Password</label>
            <input type="hashword" name="hashword" id="hashword" class="form-control" ng-model="vm.user.hashword" required />
            <span ng-show="form.hashword.$dirty && form.hashword.$error.required" class="help-block">Password is required</span>
        </div>
        <div class="form-actions">
            <button type="submit" ng-disabled="form.$invalid || vm.dataLoading" class="btn btn-primary">Register</button>
            <img ng-if="vm.dataLoading" src="data:image/gif;base64,..." />
            <a href="#/login" class="btn btn-link">Cancel</a>
        </div>
    </form>
</div>

</div>

注册控制器/功能

(function () {
    'use strict';

    angular
        .module('app')
        .controller('RegisterController', RegisterController);

    RegisterController.$inject = ['UserService', '$location', '$rootScope', 'FlashService'];
    function RegisterController(UserService, $location, $rootScope, FlashService) {
        var vm = this;

        vm.register = register;

        function register() {
            vm.dataLoading = true;
            UserService.Create(vm.user)
                .then(function (response) {
                    if (response.success) {
                        FlashService.Success('Registration successful', true);
                        $location.path('/login');
                    } else {
                        FlashService.Error(response.message);
                        vm.dataLoading = false;
                    }
                });
        return false;
        }
    }

})();

1 个答案:

答案 0 :(得分:1)

您是如何将控制器分配给视图的?你使用controller as语法了吗?例如: <div ng-controller="RegisterController as vm">your view here </div> 您正在将寄存器功能分配给vm,但vm只是控制器功能中的局部变量,因此angular无法在视图中使用其功能。 将this分配给本地vm变量是一种很好的做法,比在任何地方使用$ scope更好,但它仍然只是控制器函数范围内的局部变量。如果您将控制器分配给查看并重命名控制器(<div ng-controller="RegisterController as vm">),那么它将起作用。