简体   繁体   中英

Can any one help me on angular js

when i am loging to admin section . i am getting some issue like {{vm.error}} .

 <div class="col-md-6 col-md-offset-3 bg-success"> <h2>Admin Login</h2> <div ng-show="vm.error" class="alert alert-danger">{{vm.error}}</div> <form name="form" ng-submit="vm.admin()" role="form" novalidate> <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.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.password.$dirty && form.password.$error.required }"> <label for="password">Password</label> <input type="password" name="password" id="password" class="form-control" ng-model="vm.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-danger">Login</button> <img ng-if="vm.dataLoading" src="" /> <a href="#/register" class="btn btn-danger">Register</a> <a href="#/forgetpassword"> Forget Password </a> </div> </form> </div>

Controller admin

(function () {
'use strict';

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

AdminController.$inject = ['$location', 'AuthenticationService', 'FlashService'];
function AdminController($location, AuthenticationService, FlashService) {
    var vm = this;

    vm.admin = admin;

    (function initController() {
        // reset login status
        AuthenticationService.ClearCredentials();
    })();

    function admin() 
    {
        vm.dataLoading = true;
        AuthenticationService.Login(vm.username, vm.password, function (response) {
            if (response.success) {
                AuthenticationService.SetCredentials(vm.username, vm.password);
                $location.path('/');
            } else {
                FlashService.Error(response.message);
                vm.dataLoading = false;
            }
        });
    };
}

})();

You get {{vm.error}} displayed on screen because angular doesn't know anything about it. Your controller only sets vm.admin , so vm.error is not known.

It looks like you're setting your error into FlashService . You need to get the error out of there somehow - it won't magically put it into vm.error for you.

It appears that you're trying to copy the login approach presented here: https://github.com/cornflourblue/angular-registration-login-example

Please note that their flash message is in the main layout (shared across all views), not part of the login page. ( https://github.com/cornflourblue/angular-registration-login-example/blob/master/index.html#L14 )

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