简体   繁体   English

AngularJS-如何使用ng-hide隐藏元素?

[英]AngularJS- how to hide elements using ng-hide?

I have an app that has been written in AngularJS, and am currently trying to add the functionality to hide the headings of some widgets displayed on one of the web pages when the user selects a checkbox to indicate that they should be hidden. 我有一个用AngularJS编写的应用程序,当前正在尝试添加功能,以在用户选中复选框以指示应该隐藏它们时隐藏其中一个网页上显示的某些窗口小部件的标题。

At the moment, I have a page that displays a number of widgets- on the 'heading' of each widget, there is a 'Settings' button. 目前,我有一个页面,其中显示了许多小部件-每个小部件的“标题”上都有一个“设置”按钮。 When the user clicks the Settings button, a dialog box opens up on top of the current page (ie the user does not navigate to another page- the URL does not change at all). 当用户单击“设置”按钮时,将在当前页面的顶部打开一个对话框(即,用户不会导航到另一个页面-URL完全不会更改)。 That dialog box contains a form with a number of input fields- one of which is a checkbox that I want to use to 'hide' the headings of all of the widgets on the webpage. 该对话框包含一个带有多个输入字段的表单,其中一个是我要用来“隐藏”网页上所有小部件标题的复选框。

I have been following the example at: https://docs.angularjs.org/api/ng/directive/ngHide to try and do this, but can't quite seem to get it working... 我一直在以下示例中查看以下示例: https : //docs.angularjs.org/api/ng/directive/ngHide尝试执行此操作,但似乎无法使其正常运行...

I have added the ng-hide attribute to my HTML element, as in the example: 我已将ng-hide属性添加到我的HTML元素中,如示例所示:

<div data-ng-if="widget.name === 'tag-box'" ng-hide="hideWidgetHeading"> <!-- class="ng-hide"-->
    <div class="divider"></div>
    ...

    <div class="divider"></div>
    <div class="row ui-checkbox-row">
        <label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
        <div class="col-sm-8 col-xs-6">
            <label class="ui-checkbox">
                <input type="checkbox" name="noWidgetHeading" id="noWidgetHeading" ng-true-value= "'YES'" ng-false-value= "'NO'" ng-change="hideWidgetHeading()" ng-click="hideWidgetHeading()" ng-checked="hideWidgetHeading" ng-model="viewModel.hideWidgetHeading">
                <!-- ng-model="viewModel.hideWidgetHeading" -->
                <span></span>
            </label>
        </div>
    </div>
</div>

I defined the hideWidgetHeading() function in the ctrl.js file as follows: 我在ctrl.js文件中定义了hideWidgetHeading()函数,如下所示:

function hideWidgetHeading(){
    if($scope.widgetHeadingCheckbox==false) {
        $scope.$watch('noWidgetHeading', function() {
            $scope.hideWidgetHeading = true; 
            console.log("Value of hideWidgetHeading: ", $scope.hideWidgetHeading);
        });
        return true; 
    } else {
        console.log("hideWidgetHeading() else called (Widget/ctrl.js line 440) ");
        $scope.$watch('noWidgetHeading', function() {
            $scope.hideWidgetHeading = false; //document.getElementById('noWidgetHeading');
        });
        return false; 
    }

    if($scope.hideWidgetHeading) {
        console.log("hideWidgetHeading is true- hide the widget heading: ");

    }
    return $scope.hideWidgetHeading;
}

and I have added the following CSS to my widgets.scss file: 并且我将以下CSS添加到我的widgets.scss文件中:

.animate-show-hide.ng-hide {
  opacity: 0;
}

.animate-show-hide.ng-hide-add,
.animate-show-hide.ng-hide-remove {
  transition: all linear 0.5s;
}

.umw-tag-box {
  opacity: 1;
}

When I load my page as it is presently, when I click the Settings button, the dialog opens up. 当我按原样加载页面时,单击“设置”按钮,将打开对话框。 If I then check the 'Hide widget heading' checkbox, and click Submit, the debug that I have added displays the following in the console: 如果然后我选中“隐藏小部件标题”复选框,然后单击“提交”,则我添加的调试在控制台中显示以下内容:

Value of hideWidgetHeading: true hideWidgetHeading的值:true

which tells me that the code inside the $scope.$watch(...){...} function is running. 这告诉我$scope.$watch(...){...}函数中的代码正在运行。

However, if I click the Settings button, and then either don't check the 'Hide widget heading' checkbox, or check it and uncheck it again, and then click Submit, I get the same true value displayed in the console debug, which indicates that the code inside the $scope.$watch(...){...} function is running regardless of whether the 'watched' element changes or not. 但是,如果我单击“设置”按钮,然后不选中“隐藏小部件标题”复选框,或者选中它并再次取消选中它,然后单击“提交”,那么我将在控制台调试中得到相同的true值,指示$scope.$watch(...){...}函数中的代码正在运行,无论'watched'元素是否更改。

Questions 问题

  1. How can I ensure that the code inside the $scope.$watch(...){...} only runs when the 'watched' element (ie the checkbox) has its value changed? 如何确保$scope.$watch(...){...}仅在'watched'元素(即复选框)的值更改时才能运行?

  2. How do I actually 'call' the CSS that I've added to hide the HTML elements on the particular HTML that I want to hide? 我实际上如何“调用”我添加的CSS,以隐藏要隐藏的特定HTML上的HTML元素? Or how do I make that CSS 'apply' to the HTML elements that I want to hide? 或如何使CSS“应用”到要隐藏的HTML元素?

Edit 编辑

I changed the HTML for the checkbox to: 我将复选框的HTML更改为:

<input type="checkbox" ng-model="checked" name="noWidgetHeading" id="noWidgetHeading">

as suggested, and when I now browse to my page, and open the dialog, it displays the widget as I expect: 根据建议,当我现在浏览至页面并打开对话框时,它将按我的期望显示小部件:

小部件

When I click the 'Settings' button on the widget, the 'Configure Item' dialog opens up on top of the page: 当我单击小部件上的“设置”按钮时,“配置项”对话框将在页面顶部打开:

配置项目对话框

But when I select the 'Hide widget heading' checkbox, it actually hides the 'Tag' label & input box from the dialog: 但是,当我选择“隐藏小部件标题”复选框时,它实际上从对话框中隐藏了“标签”标签和输入框:

复选框隐藏标签元素

The element I want to hide is actually displayed on the page from which the dialog box is opened, not on the dialog box itself... but I can't seem to work out how I can hide that using a control on the dialog... Any suggestions? 我要隐藏的元素实际上显示在打开对话框的页面上,而不显示在对话框本身上……但是我似乎无法弄清楚如何使用对话框上的控件隐藏该元素。 .. 有什么建议么?

So, this should work, it is even the first example shown in their docs , with your code it will be: 因此,这应该可行,这甚至是其文档中显示第一个示例 ,您的代码将是:

<div data-ng-if="widget.name === 'tag-box'" ng-hide="viewModel.hideWidgetHeading"> <!-- class="ng-hide"-->
    <div class="divider"></div>
    ...

    <div class="divider"></div>
    <div class="row ui-checkbox-row">
        <label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
        <div class="col-sm-8 col-xs-6">
            <label class="ui-checkbox">
                <input type="checkbox" name="noWidgetHeading" id="noWidgetHeading" ng-true-value= "'YES'" ng-false-value= "'NO'" ng-change="logWidgetHeading()" ng-click="logWidgetHeading()" ng-checked="viewModel.hideWidgetHeading" ng-model="viewModel.hideWidgetHeading">
                <!-- ng-model="viewModel.hideWidgetHeading" -->
                <span></span>
            </label>
        </div>
    </div>
</div>

you can keep functions on the events if you wan to log values, but there is no need for hiding. 如果您想记录值,则可以保留事件的功能,但无需隐藏。 The ng-model directive will update your value and the ng-hide should follow. ng-model指令将更新您的值,并且ng-hide应该紧随其后。

function logWidgetHeading(){
    console.log("Value of hideWidgetHeading: ", $scope.hideWidgetHeading);
}

What i was saying about function or var: in some cases, i used to have values from the scope that were not updated, and the solution was to introduce a function to be called to retreive the value. 我所说的关于function或var的意思是:在某些情况下,我曾经拥有范围中未更新的值,而解决方案是引入一个要调用的函数以获取该值。 It is what i thought you were trying because ng-hide="hideWidgetHeading" shows the same name as your function: function hideWidgetHeading(){ , that's why i first said that the round brackets were missing. 这就是我认为您要尝试的原因,因为ng-hide="hideWidgetHeading"显示的名称与函数名称相同: function hideWidgetHeading(){ ,这就是为什么我首先说缺少圆括号的原因。 So another version would be something like this (without ng-model on purpose, to show an alternate way to modify stuff with your events): 因此,另一个版本将是这样的(故意不使用ng-model,以显示使用事件修改内容的另一种方法):

<div data-ng-if="widget.name === 'tag-box'" ng-hide="getHideWidgetHeading()"> <!-- class="ng-hide"-->
    <div class="divider"></div>
    ...

    <div class="divider"></div>
    <div class="row ui-checkbox-row">
        <label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
        <div class="col-sm-8 col-xs-6">
            <label class="ui-checkbox">
                <input type="checkbox" name="noWidgetHeading" id="noWidgetHeading" ng-true-value= "'YES'" ng-false-value= "'NO'" ng-change="toggleWidgetHeading()" ng-checked="isHiddenWidgetHeading">
                <!-- ng-model="viewModel.hideWidgetHeading" -->
                <span></span>
            </label>
        </div>
    </div>
</div>

and the js: 和js:

//initialisation to the default value:
$scope.isHiddenWidgetHeading = false;

//function that toggles the value:
$scope.toggleWidgetHeading = function(){
    $scope.isHiddenWidgetHeading = !$scope.isHiddenWidgetHeading;
};

//function to retreive the value:
$scope.getHideWidgetHeading = function(){
    return $scope.isHiddenWidgetHeading;
};

Sorry i was a bit quick in naming vars and such, but you should get what you need here.. 抱歉,我在命名var等时有点快,但是您应该在这里得到所需的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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