简体   繁体   English

Angular JS:基于选择选项显示/关闭div而不使用控制器或模型

[英]Angular JS : Show/Close div based on select option without using controller or model

I am new to Angular JS. 我是Angular JS的新手。 I want to show/hide div based on select option but without using controller or model. 我想基于选择选项显示/隐藏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> 

what i want is . 我要的是。

when i select button with file then file div should be open and others should be closed and when i select button with link then textbox should open. 当我与文件选择按钮,然后文件格应该是开放的和其他应关闭,当我与链接选择按钮,然后文本框应该打开。

I can do using controller and model, but I want to do it without using them. 我可以使用控制器和模型,但是我不想使用它们。

so is there any way so that I can do that? 有什么办法可以做到吗?

Thank You. 谢谢。

As per your comments I find you don't want to use Controller or model to do your task. 根据您的评论,我发现您不想使用Controller或model来完成任务。 I wonder why use angular then!! 我不知道为什么要用角形!

Anyways, you must understand that model and Controller are correlated. 无论如何,您必须了解模型和Controller是关联的。 Go through this , it will clarify a lot of things for you. 通过此过程 ,它将为您澄清很多事情。

However, if you don't want to write the logic in controller, you can go ahead with a combination of ng-model and ng-switch as follows, 但是,如果您不想在控制器中编写逻辑,则可以继续使用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> 

Yeah, sure, it's a trivial piece of jQuery. 是的,当然,这是一部琐碎的jQuery。 Demonstrated here https://codepen.io/anon/pen/zojPKa , but the basis is just a change() and a switch. 在此处https://codepen.io/anon/pen/zojPKa进行了演示,但是基础只是一个change()和一个switch。

$("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;
  }
});

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

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