简体   繁体   中英

Angular js ng-show not working properly

I have a function that executes when my html form is submitted, if there are errors while processing the form on the server the errors are returned as a json. I want to display these errors to the user, but for some reason the ng-show on my error div container never displays.

Html code:

<form class="form-horizontal" role="form" name="registerForm" ng-controller="RegisterController as register" ng-submit="registerForm.$valid && register()" novalidate>
        <div class="form-group">
            <div class="col-md-3">
                <label for="email">Email: </label>
            </div>

            <div class="col-md-9">
                <input type="email" id="email" name="email" ng-model="d.email" class="form-control" ng-required/>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-3">
                <label for="password">Password</label>
            </div>

            <div class="col-md-9">
                <input type="password" id="password" name="password" ng-model="d.password" class="form-control" ng-required/>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-3">
                <label for="confPassword">Confirm Password</label>
            </div>

            <div class="col-md-9">
                <input type="password" id="password_confirmation" name="password_confirmation" ng-model="d.password_confirmation" nx-equal-ex="d.password" class="form-control" ng-required/>
            </div>
        </div>

        <span class="help-block errorMessages" ng-show="d.errors.length">
            <div class="alert alert-danger">
                <strong>Whoops!</strong> There were some problems with your input.<br><br>
                <ul class="list">
                    <li ng-repeat="error in d.errors"><% error %></li>
                </ul>
            </div>
        </span>

        <div class="form-group">
            <div class="col-md-12">
                <br/>
                <hr>
                <button class="big-red-button" type="submit">Register <i class="glyphicon glyphicon-chevron-right"></i></button>
            </div>
        </div>
    </form>

Register.js Controller:

(function() {

    angular.module('vendor').controller('RegisterController', ['$http', '$scope', function($http, $scope) {

        $scope.d = {};
        $scope.d.errors = {};

        $scope.register = function() {

            $.ajax({
                type: "POST",
                url: "/auth/register",
                headers : {
                    'X-XSRF-Token': $("meta[name='csrf_token']").attr("content")
                },
                data: {
                    email: $scope.d.email,
                    password: $scope.d.password,
                    password_confirmation: $scope.d.password_confirmation
                },
                dataType: 'json',
                success: function(data) {
                    alert(result[0]);
                },
                error: function(xhr, status, err) {
                    var responseJSON = JSON.parse(xhr.responseText);
                    $scope.d.errors = responseJSON;
                    //alert($scope.d.errors.name);

                }
            });
        };

    }]);

}) ();

Nothing happens when the errors are returned to the page, ng-show does not display the code.

Use data binding to show the errors:

<ul class="list">
 <li ng-repeat="error in d.errors"><% error %>{{error}}</li>
</ul>

I might be wrong but $.ajax is a jQuery? If so,

Anything outside angular universe needs to trigger digest cycle manually.

So you got two options.

after $scope.d.errors = responseJSON add

$scope.$apply();

Or use $http

 $http.post({
   type: "POST",
   url: "/auth/register",
   headers : {
     'X-XSRF-Token': $("meta[name='csrf_token']").attr("content")
   },
   data: {
     email: $scope.d.email,
     password: $scope.d.password,
     password_confirmation: $scope.d.password_confirmation
   }
 }).success(
   function(data) {
     alert(data[0]);
   }
 ).error(function(err, status, headers, conf) {
     $scope.d.errors = data;
     //alert($scope.d.errors.name);
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM