从angularjs控制器调用时显示html

时间:2016-08-31 07:42:46

标签: javascript html angularjs controller

我知道可以找到类似的问题,但没有答案对我有用。我想在登录时遇到错误的凭据时显示一个html,例如<h5>Your password is incorrect</h5>。所以这段代码应该是#34;活着的#34;从控制器内部。以下是我的看法:

( function () {
    'use strict';

    angular
        .module( 'app.home' )
        .controller( 'ploginController', ploginController );

    ploginController.$inject = [ '$scope', '$location', '$state', '$http' ];

    /* @ngInject */
    function ploginController( $scope, $location, $state, $http ) {

        $scope.submit = function () {
            $http.post( '/api/v1/person/login', $scope.add, {
                headers: {
                    'Content-Type': 'application/json'
                }
            } ).then( function ( respSucc ) {
                $state.go( 'layout.listcampaigns' );
                return respSucc;
            }, function ( respErr ) {
//i think code revealing method should be HERE, but how??
                return respErr;
            } );
        };
    }

} )();

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以使用ng-if

首先,在您的控制器中:

$scope.loginIncorrect = false;

...

$scope.submit = function () {
    $http.post( '/api/v1/person/login', $scope.add, {
        headers: {
            'Content-Type': 'application/json'
        }
    } ).then( function ( respSucc ) {
        $scope.loginIncorrect = false;
        $state.go( 'layout.listcampaigns' );
        return respSucc;
    }, function ( respErr ) {
        $scope.loginIncorrect = true;
        return respErr;
    } );
};

然后在你的HTML中:

<h5 ng-if="loginIncorrect">Your password is incorrect</h5>

希望它有所帮助。

答案 1 :(得分:0)

你能试试吗??

<h5 ng-show="showError">Your password is incorrect</h5>

在你的控制器内:

  function ploginController( $scope, $location, $state, $http ) {
        $scope.showError =false;
        $scope.submit = function () {
            $http.post( '/api/v1/person/login', $scope.add, {
                headers: {
                    'Content-Type': 'application/json'
                }
            } ).then( function ( respSucc ) {
                 $scope.showError =false;
                $state.go( 'layout.listcampaigns' );
                return respSucc;
            }, function ( respErr ) {
                  $scope.showError =true;
//i think code revealing method should be HERE, but how??

            } );
        };
    }