【问题标题】:ng-if does't hide the second elementng-if 不隐藏第二个元素
【发布时间】:2015-10-17 18:49:52
【问题描述】:

我每两个视图和一个服务有两个控制器。服务对象存储从第一个视图中选择的值。基于此选定值ng-if 必须在第二个视图上呈现任一元素。这是我应该执行此任务的代码。

第一个视图的控制器:

gasStation.controller('registrationController', ['$scope', '$log', '$http', '$window', 'customerType', function ($scope, $log, $http, $window, customerType) {
    customerType.setCustomerType($scope.customerType);
    url += '/pages/index.html#' + '/dashboard';
    $window.location.href = url;
    $scope.customerType = customerType.getCustomerType();
    console.log(customerType.getCustomerType() + ' customer type from controller');};}]);

第一个视图:

    <div class="site-content">
        <h1 class="fueling-header">Registration</h1>

        <form name="regForm" class="pure-form pure-form-aligned">
            <fieldset>

                <!--<%--Type of customers--%>-->
                <div class="pure-control-group">
                    <label for="customerTypes">Your group</label>
                    <select id="customerTypes" ng-model='customerType'>
                        <option value="REGULAR">Regular</option>
                        <option value="BUSINESS">Business</option>
                    </select>
                    <label id="groupError" class="error"/>
                    {{customerType}}
                </div>
            </fieldset>
        </form>
    <!--END: register page-->

第二个控制器

gasStation.controller('dashboardController', ['$scope', '$log', 'customerType', 
function ($scope, $log, customerType) {
    $scope.customerType = customerType.getCustomerType();
    $scope.isBusiness = (customerType.getCustomerType() === 'BUSINESS');
    $scope.isRegular = (customerType.getCustomerType() === 'REGULAR');
    console.log($scope + ' dashboard controller');}]);

第二个视图:

<div id="menu">
     <div class="pure-menu menu-width-restricted">
        <span class="pure-menu-heading">
            <a href="/">GSS <img src="/resources/img/gss-icon.png" width="24" height="24"/></a>
        </span>

        <ul class="pure-menu-list">                                  

        <li class="fixed-height">
        <br/>

        <div class="pure-menu-link">
            Hello, {{customerType}}
        </div>

        <div ng-if="isRegular">

            <li class="fixed-height">
                <a href="#/business_dashboard" class="pure-menu-link">{{customerType == 'REGULAR'}}</a>
            </li>
            <li class="fixed-height">
                <a href="#/business_dashboard" class="pure-menu-link">Revenue</a>
            </li>

            <li class="fixed-height">
                <a href="#/view_gasstations" class="pure-menu-link">Stations</a>
            </li>

        </div>

        <div ng-if="isBusiness">

            <li class="fixed-height">
                <a href="#/search_gasstations" class="pure-menu-link">Search</a>
            </li>

            <li class="fixed-height">
                <a href="#/view_fuelings" class="pure-menu-link">Fuelings</a>
            </li>

            <li class="fixed-height">
                <a href="#/view_vehicles" class="pure-menu-link">Vehicles</a>
            </li>

        </div>

        <br/>
        </li>

        <li class="fixed-height">
        <hr/>
        </li>
        <br/>
        <li class="fixed-height">
        <a href="#/logout" class="pure-menu-link">Log Out</a>
        </li>
        </ul>
    </div>
</div>

当我从第一个视图的下拉列表中选择 REGULAR 类型的客户时,在第二个视图中显示 &lt;div ng-if="isRegular"&gt;&lt;/div&gt; 而未显示 &lt;div ng-if="isBusiness"&gt;&lt;/div&gt;

问题是当我从第一个视图中选择业务类型客户时,&lt;div ng-if="isBusiness"&gt;&lt;/div&gt;&lt;div ng-if="isRegular"&gt;&lt;/div&gt; 会同时显示。但我只想显示&lt;div ng-if="isBusiness"&gt;&lt;/div&gt;

有什么问题,我该如何解决?

我选择普通客户时的堆栈跟踪屏幕截图:

当我选择业务类型客户时:

【问题讨论】:

    标签: javascript jquery html css angularjs


    【解决方案1】:

    当您编辑您的选择时,您的 $scope.customerType 会更改,而不一定是您的 customerType.getCustomerType() 方法。 我不确定为什么你的不起作用,因为我不知道 getCustomerType() 到底是做什么的。

        $scope.customerType = customerType.getCustomerType();
        $scope.isBusiness = (customerType.getCustomerType() === 'BUSINESS');
        $scope.isRegular = (customerType.getCustomerType() === 'REGULAR');
    

    您是否检查过控制台中的错误?它可能不会评估 ng-if,如果发生错误,只会显示两者。

    【讨论】:

    • 我将控制台日志放在第二个控制器上。我看到的是:1.服务对象正确地从第一个视图传输信息,2.第二个控制器的 $scope 包含客户类型。但是,ng-if 仍然无法正常工作。 PS 我已经添加了堆栈跟踪的屏幕截图。
    • 我不确定您要在注册控制器中做什么。您是在控制器内立即重定向吗?似乎如果您在该注册视图中更改您的选择,则什么也没有发生。给定您的代码。
    • 是的,我在我的注册控制器中进行了立即重定向。我已经解决了我的问题。看来问题出在我的仪表板视图中。
    • 我无法解释为什么这样的标记对我很有效。请在下面查看我的答案。
    • 您的 ng-if 创建了一个子作用域,这意味着 customerType 变量在所有作用域中可能并非始终相等。我会尝试以您以前的方式输出 customerType 的值,以查看发生了什么。不需要您当前的解决方案。
    【解决方案2】:

    我希望我能解释为什么这种布局会根据仪表板控制器的布尔值正确呈现。

    <div id="menu">
     <div class="pure-menu menu-width-restricted">
    
        <div ng-if="isRegular()">
            <span class="pure-menu-heading">
                <a href="/">GSS <img src="/resources/img/gss-icon.png" width="24" height="24"/></a>
            </span>
    
            <ul class="pure-menu-list">                                  
    
                <li class="fixed-height">
                <br/>
    
                <div class="pure-menu-link">
                    Hello, {{customerType}}
                </div>
    
                <li class="fixed-height">
                    <a href="#/search_gasstations" class="pure-menu-link">Search</a>
                </li>
    
                <li class="fixed-height">
                    <a href="#/view_fuelings" class="pure-menu-link">Fuelings</a>
                </li>
    
                <li class="fixed-height">
                    <a href="#/view_vehicles" class="pure-menu-link">Vehicles</a>
                </li>
    
                <br/>
    
                <li class="fixed-height">
                <hr/>
                </li>
    
                <br/>
    
                <li class="fixed-height">
                <a href="#/logout" class="pure-menu-link">Log Out</a>
                </li>
    
            </ul>
    
        </div>
    
        <div ng-if="isBusiness()">
            <span class="pure-menu-heading">
                <a href="/">GSS <img src="/resources/img/gss-icon.png" width="24" height="24"/></a>
            </span>
    
            <ul class="pure-menu-list">                                  
    
                <li class="fixed-height">
                <br/>
    
                <div class="pure-menu-link">
                    Hello, {{customerType}}
                </div>
    
                <li class="fixed-height">
                    <a href="#/business_dashboard" class="pure-menu-link">Revenue</a>
                </li>
    
                <li class="fixed-height">
                    <a href="#/view_gasstations" class="pure-menu-link">Stations</a>
                </li>
    
                <br/>
    
                <li class="fixed-height">
                <hr/>
                </li>
    
                <br/>
    
                <li class="fixed-height">
                <a href="#/logout" class="pure-menu-link">Log Out</a>
                </li>
    
            </ul>
    
        </div>
    
    </div>
    

    【讨论】:

      猜你喜欢
      • 2013-03-22
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 2021-09-22
      • 1970-01-01
      相关资源
      最近更新 更多