2016-12-07 3 views
0

Angular JSを初めて使用しています。選択肢に基づいてコントローラやモデルを使わずにdivを表示/非表示したい。私が欲しいものAngular JS:コントローラまたはモデルを使用せずに選択オプションに基づいて表示/閉じるdiv

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<select class=""> 
 
                <option class="add_btn_with_file">Add button with file</option> 
 
                <option class="add_btn_with_link">Add button with link</option> 
 
                <option class="add_btn_with_text">Add button with text </option> 
 
               </select> 
 

 

 
<div class=""> 
 

 
               <input type="file" id="browse" name="fileupload" class="file_display_none" /> 
 
               
 
              </div> 
 

 

 
<div class=""> 
 
    <input type="text" placeholder="button with link" ></div>

です。私は、ファイルとボタンを選択すると

その後、ファイル divが開いている必要があり、他は閉じられなければならないと私はリンクボタンを選択すると、その後テキストボックスが開きます。

私はコントローラーとモデルを使用することができますが、私はそれらを使用せずにやりたいです。

私はそれを行うことができるように何か方法はありますか?

ありがとうございます。

+0

それはNG-モデルがあなたのための変更を監視するために、あなただけの、それを使用する必要はできません –

+0

いいのでNG-モデルは右..選択オプションに基づいて値を変更する状態を保ちますか? –

+0

try ng-switch .... http://www.w3schools.com/angular/tryit.asp?filename=try_ng_ng-switch – Chetan

答えて

3

あなたのコメントによると、コントローラやモデルを使用してタスクを実行したくないとわかります。私はなぜ角度を使用するのだろうかと思う!

とにかく、モデルとコントローラが関連付けられていることを理解しておく必要があります。 thisを通過すると、多くのことが明らかになります。

あなたはコントローラ内のロジックを記述したくない場合は、あなたがng-modelng-switch次のように組み合わせを進めることができ、

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<select class="" ng-model="fileSelect"> 
 
                <option class="add_btn_with_file" value="file">Add button with file</option> 
 
                <option class="add_btn_with_link" value="link">Add button with link</option> 
 
                <option class="add_btn_with_text" value="text">Add button with text </option> 
 
               </select> 
 

 
<div ng-switch="fileSelect"> 
 
<div class="" ng-switch-when="file"> 
 

 
               <input type="file" id="browse" name="fileupload" class="file_display_none" /> 
 
               
 
              </div> 
 

 

 
<div class="" ng-switch-when="text"> 
 
    <input type="text" placeholder="button with link" ></div> 
 

 
<div class="" ng-switch-default> 
 

 
               <input type="file" id="browse" name="fileupload" class="file_display_none" /> 
 
               
 
              </div> 
 
</div>

+0

は機能しません。デフォルトでは**ファイル付きのボタンを追加する必要があります**他のものは非表示にし、選択に基づいて、選択値から関連するdivを表示する必要があります。 –

+0

私の答えを修正する! –

+0

大丈夫です...大丈夫です... –

-2

確認、うん、それはjQueryのほんの一部です。ここでは https://codepen.io/anon/pen/zojPKaで説明されていますが、基本はchange()とスイッチです。

$("select.foo").change(function(){ 
    var selected = $("select.foo").val(); 
    switch(selected){ 
    case "Add button with file": 
     $("#fileInput").show(); 
     $("#textInput").hide(); 
     break; 
    case "Add button with text": 
     $("#fileInput").hide(); 
     $("#textInput").show(); 
     break; 
    default: 
     $("#fileInput, #textInput").hide(); 
     break; 
    } 
}); 
+0

私は ' jQueryを使いたい。 –

+0

@Rishiコントローラ、モデル、またはjQueryを使用したくない場合は、おそらくバニラJSで上記の方法を実行することができます。私はあなたにそれを残します。 – darthbob88