我試圖提高使用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