javascript - 从 angularjs Controller 调用时显示 html

标签 javascript html angularjs controller

我知道可以找到类似的问题,但没有答案对我有用。我想显示一个html,例如<h5>Your password is incorrect</h5>当我在登录时遇到错误的凭据时。所以这段代码应该从 Controller 内部“激活”。这是我的样子:

( 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;
            } );
        };
    }

} )();

提前致谢!

最佳答案

您可以使用ng-if来实现这一点。

首先,在您的 Controller 中:

$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>

希望有帮助。

关于javascript - 从 angularjs Controller 调用时显示 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39243579/

相关文章:

javascript - 如何在菱形 div 中居中图像? - CSS,HTML

javascript - AngularJS : Service not Returning Variables to Controllers

javascript - 从一个巨大的数组中获取数据

javascript - if/else 列出可能的日期条件永远不会成功

java - AJAX Play Framework

css - 使用浏览器大小调整 div 的大小? (不是一个简单的问题)

javascript - 我如何检查 parent 在 html 中是 span 还是 anchor?

html - html属性中的vuejs变量

javascript - 如何从 Controller 返回总计到 <td> 标签 Angular JS

javascript - 让同步代码等待异步调用