简体   繁体   English

制作不带ng-show或ng-hide的AngularJS“隐藏并显示”解决方案

[英]Making an AngularJS “hide and show” solution without ng-show or ng-hide

Want to hide or show elements on AngularJS based on a drop-down (select), but since the page is a mashup with QlikSense the elements from Qlik doesn't work well with ng-show. 想要基于下拉菜单(选择)在AngularJS上隐藏或显示元素,但是由于该页面是QlikSense的混搭,因此Qlik中的元素不适用于ng-show。 I tried this approach with ng-show: http://jsfiddle.net/3ernardo/EZbfM/873/ but the elements are only partially displayed. 我通过ng-show尝试了这种方法: http : //jsfiddle.net/3ernardo/EZbfM/873/,但是元素仅部分显示。 So I've made a version based on child visibility with CSS, now the elements are properly displayed but the problem is that as the page loads all options are visible and the drop-down have no option selected. 因此,我已经基于CSS的子可见性创建了一个版本,现在可以正确显示元素了,但是问题是,随着页面加载,所有选项都可见,并且下拉菜单中没有选择任何选项。 I need this drop-down to work like the previous one that had an option selected and just it's correct element visible on load. 我需要此下拉菜单才能像上一个选择了选项的下拉菜单一样工作,只是它在加载时可见。 Here is a fiddle for this second attempt: https://jsfiddle.net/3ernardo/s6ycapb0/ 这是第二次尝试的小提琴: https : //jsfiddle.net/3ernardo/s6ycapb0/

<Style>
div.class1 div.child,
div.class2 div.child,
div.class3 div.child,
div.class4 div.child {
  display: none;
}

div.class1 div:nth-child(1),
div.class2 div:nth-child(2),
div.class3 div:nth-child(3),
div.class4 div:nth-child(4) {
  display: block;
}
</Style>
<div ng-app="">
  <select ng-model="visibility">
    <option value="class1" ng-selected="true">Red Class</option>
    <option value="class2">Green Class</option>
    <option value="class3">Blue Class</option>
    <option value="class4">Yellow Class</option>
  </select>

  <div ng-class="visibility">
    <div class="child">
      <p>Element from class 1.</p>
    </div>
    <div class="child">
      <p>Element from class 2.</p>
    </div>
    <div class="child">
      <p>Element from class 3.</p>
    </div>
    <div class="child">
      <p>Element from class 4.</p>
    </div>
  </div>
</div>

I am a Qlik Developer also and had a similar problem with elements being rendered before the controller had initialised. 我也是Qlik开发人员,在控制器初始化之前渲染元素时遇到了类似的问题。 I added the following as a class to the offending objects 我将以下内容作为类添加到有问题的对象中

 .hidden {
    visibility: hidden;
    }  

and then used ng-hide="something" on the element 然后在元素上使用ng-hide="something"

I defined something in my angular controller and so it would show once the angular was loaded if i set something to false and would remain hidden if i set something to true. 我在角度控制器中定义了一些内容,因此如果我将内容设置为false,它将在角度加载后显示,如果将内容设置为true,它将保持隐藏状态。

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

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