简体   繁体   English

我们如何使用ng-show和ng-hide在简单的条件下管理多个条件

[英]how can we manage multiple conditions in a simple condition using ng-show and ng-hide

I am trying to improve multiple conditions using ng-show and ng-hide using directives, here is my code 我正在尝试使用ng-show和ng-hide使用指令来改善多个条件,这是我的代码

html code HTML代码

 <my-directive controls="a,b,c"></my-directive>

js code js代码

.directive('myDirective',function(){
  return{
    restrict:'E',
    templateUrl:"parentHtml.html",
    link:function(scope,elem,attr){
      var options = attr.controls;
      if(options=="a,b,c"){
        scope.showMeAll=true;
      }else if(options=="a"){
        scope.showmeA=true;
      }else if(options=="b"){
        scope.showmeB=true;
      }else if(options=="c"){
        scope.showmeC=true;
      }
    }
  }
}).directive('subDirective',function(){
  return{
    restrict:'E',
    template:"<h2>aapple</h2>",
    link:function(scope,elem,attr){

    }
  }
}).directive('subDirective1',function(){
  return{
    restrict:'E',
    template:"<h2>BBatt</h2>",
    link:function(scope,elem,attr){

    }
  }
}).directive('subDirective2',function(){
  return{
    restrict:'E',
    template:"<h2>CCat</h2>",
    link:function(scope,elem,attr){

    }
  }
});

here is my parentHtml.html code 这是我的parentHtml.html代码

<div class="row">
  <div ng-show="showMeAll">
    <sub-directive></sub-directive>
    </div>
   <div ng-show="showMeB">
    <sub-directive1></sub-directive1>
    </div>
    <div ng-show="showMeC">
    <sub-directive2></sub-directive2>
    </div>
</div>

My problem is when I give all the three "a,b,c" to the directive attribute then in "parentHtml" all the three div's have to show, if I give only two ie "a,b" then in parentHtml only two div's have to show ie "apple" and "bat" and also if give only one string ie "c" then in parentHtml only "cat" div have to show, in a simple way if what the alphabet I give to the directive attribute thet div have to show. 我的问题是当我将所有三个“a,b,c”赋予指令属性然后在“parentHtml”中所有三个div必须显示,如果我只给出两个即“a,b”那么在parentHtml中只有两个div的必须显示即“苹果”和“蝙蝠”,如果只给出一个字符串,即“c”,那么在parentHtml中只有“cat”div必须以简单的方式显示我给指令属性thet div的字母表是什么必须表明。 Here is my http://plnkr.co/edit/6gAyAi63Ni4Z7l0DP5qm?p=preview . 这是我的http://plnkr.co/edit/6gAyAi63Ni4Z7l0DP5qm?p=preview Please solve my question in a simple way. 请以简单的方式解决我的问题。

Thanks In Advance 提前致谢

All your divs that wrap directives have ng-show, so the code should be : 包装指令的所有div都有ng-show,所以代码应该是:

if(options=="a,b,c"){
    scope.showMeAll=true;
    scope.showMeA=true;
    scope.showMeB=true;
    scope.showMeC=true;
 }

By setting ng-show to true to the parnet div, wont display other child divs that have ng-show. 通过将ng-show设置为true到parnet div,不会显示具有ng-show的其他子div。 Child divs have independent ng-show from parent. 儿童div具有来自父母的独立ng-show。

You have some spelling errors, instead of showmeA, showmeB, etc. it should be showMeA, showMeB, etc. me with a uppercase M . 你有一些拼写错误,而不是showmeA, showmeB, etc.它应该是showMeA, showMeB, etc. me用大写M

Beside that, your checks don't make sense, you're using if..else if checks which means that the evaluation will stop as soon as a condition is true. 除此之外,你的检查没有意义,你正在使用if..else if检查意味着评估将在条件成立时立即停止。

Also, in this case, you should check if the value of the conditions attribute contains a letter instead of if it is equal to a letter. 此外,在这种情况下,您应检查conditions属性的值是否包含字母,而不是等于字母。

Here is a working version of your directive: 这是您的指令的工作版本:

directive('myDirective',function(){
  return{
    restrict:'E',
    templateUrl:"parentHtml.html",
    link:function(scope,elem,attr){
      var options = attr.controls;
      if(options.indexOf("a") !== -1){
        scope.showMeA=true;
      }
      if(options.indexOf("b") !== -1){
        scope.showMeB=true;
      }
      if(options.indexOf("c") !== -1){
        scope.showMeC=true;
      }
      scope.showMeAll = scope.showMeB && scope.showMeA && scope.showMeC;
  }
 }
})

and HERE is a demo 这里是一个演示

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

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