我试图提高使用NG-展示和NG-隐藏使用指令多个条件中一个简单的状态管理多个条件,这里是我的代码怎样才能用NG-展示和NG-隐藏
HTML代码
<my-directive controls="a,b,c"></my-directive>
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){
}
}
});
她e是我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>
我的问题是,当我给所有三个“A,B,C”的指令属性,那么在“parentHtml”所有三个div的有是否显示,我只给出两个即“a,b”,然后在parentHtml中,只有两个div必须显示,即“apple”和“bat”,如果只给出一个字符串即“c”,那么在parentHtml中只有“cat”div必须显示,一个简单的方法,如果我给的指令属性thet div的字母表必须显示。这是我的http://plnkr.co/edit/6gAyAi63Ni4Z7l0DP5qm?p=preview。 请用简单的方法解决我的问题。
在此先感谢
而不是检查是否'controls'属性的值等于一个字母如:'选项= =“a”'你应该检查它是否包含该字母'options.indexOf(“a”)!== -1'。 – Titus
这是一个工作版本http://plnkr.co/edit/ERdku20aXECxbP30OlP3?p=预览旁边的if(...)else if(...)...'你也有一些拼写错误的错误,你正在使用'showme ...'而不是'showMe ...'。 – Titus