Angularjs给出了错误的回应

时间:2015-09-27 08:03:06

标签: javascript angularjs

我在注册控制器中的响应有问题。 由于某种原因,即使用户保存到数据库,结果也总是注册失败。为什么会这样呢?我对棱镜有点新鲜 - 春天并不明白为什么会这样?

user.service.js

(function () {
    'use strict';

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

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

        service.Create = Create;


        return service;

        function Create(user) {

            console.log(user.password)
            console.log(user.lastName)
            console.log(user.email)
            console.log(user.firstName)
            console.log(user.customfield)

            return $http.post('/api/register', user)
        }

})();

并注册。控制器

(function () {
    'use strict';

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

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

        vm.register = register;

        function register() {
            vm.dataLoading = true;
            UserService.Create(vm.user)
                .then(function (response) {
                    if (response.success) {

                        console.log("register Succeed")

                        //FlashService.Success('Registration successful', true);
                        $location.path('/home');

                    } else {

                        console.log("register Failed")
                        vm.dataLoading = false;
                    }
                });
        }
    }

})();

register.html

<div class="container" ng-controller="RegisterController as vm">
    <div class="col-md-6 col-md-offset-3">
        <h2>Register</h2>
        <div ng-show="vm.error" class="alert alert-danger">{{vm.error}}</div>
        <form name="form" ng-submit="vm.register()" role="form">


            <div class="form-group" ng-class="{ 'has-error': form.lastName.$dirty && form.lastName.$error.required }">
                <label for="lastName">Last name</label>
                <input type="text" name="lastName" id="lastName" class="form-control" ng-model="vm.user.lastName" required />
                <span ng-show="form.lastName.$dirty && form.lastName.$error.required" class="help-block">Last name is required</span>
            </div>

            <div class="form-group" ng-class="{ 'has-error': form.firstname.$dirty && form.firstname.$error.required }">
                <label for="firstName">First name</label>
                <input type="text" name="firstName" id="firstName" class="form-control" ng-model="vm.user.firstName" required />
                <span ng-show="form.firstName.$dirty && form.firstName.$error.required" class="help-block">First name is required</span>
            </div>


            <div class="form-group" ng-class="{ 'has-error': form.customfield.$dirty && form.customfield.$error.required }">
                <label for="customfield">Custom field</label>
                <input type="text" name="customfield" id="customfield" class="form-control" ng-model="vm.user.customfield" required />
                <span ng-show="form.customfield.$dirty && form.customfield.$error.required" class="help-block">customfield is required</span>
            </div>

            <div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
                <label for="email">Username</label>
                <input type="text" name="email" id="email" class="form-control" ng-model="vm.user.email" required />
                <span ng-show="form.email.$dirty && form.email.$error.required" class="help-block">email is required</span>
            </div>


            <div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
                <label for="password">Password</label>
                <input type="password" name="password" id="password" class="form-control" ng-model="vm.user.password" required />
                <span ng-show="form.password.$dirty && form.password.$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>
                <a href="#/login" class="btn btn-link">Cancel</a>
            </div>
        </form>
    </div>
</div>

2 个答案:

答案 0 :(得分:2)

根据the documentation

  

响应对象具有以下属性:

     
      
  • data - {string | Object} - 使用转换函数转换的响应体。
  •   
  • status - {number} - 响应的HTTP状态代码。
  •   
  • headers - {function([headerName])} - Header getter function。
  •   
  • config - {Object} - 用于生成请求的配置对象。
  •   
  • statusText - {string} - 响应的HTTP状态文本。
  •   

您正在测试response.success,这不是文档所说的属性。请改为status

  

200到299之间的响应状态代码被视为成功状态,将导致调用成功回调。

答案 1 :(得分:0)

至于角度承诺成功的测试,您不需要检查if语句。 promise的then函数有参数(函数),第一个用于调用成功,第二个用于失败,如:

 UserService.Create(vm.user)
    .then(function(response) {
    // this callback will be called asynchronously
    // when the response is available, i.e. success
  }, function(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });