简体   繁体   English

angular js 中 angular select 选项的条件?

[英]Condition for angular select option in angular js?

In my angular project, I have a select element with an option.在我的 angular 项目中,我有一个带有选项的 select 元素。 for option, I used ng-repeat for looping purposes.作为选项,我使用 ng-repeat 进行循环。 so in the option, I have four different options.所以在选项中,我有四个不同的选项。 and I have four different boxes.我有四个不同的盒子。 if I select any option based on that those boxes will hide and show.如果我 select 任何基于这些框的选项将隐藏和显示。 how to do this?这个怎么做? anyone please help me to fix this.任何人请帮我解决这个问题。

This is my HTML code:这是我的 HTML 代码:

<select select2 id="environment" name="environment"
              ng-model='filter.environment' data-live-search="true"
        <option ng-repeat="(key,value) in authorizationtype" value="{{key}}"
                ng-bind="value"></option>
      </select>

HTML box codes: HTML 箱码:

<div class="boxone"></div>
<div class="boxtwo"></div>
<div class="boxthree"></div>
<div class="boxfour"></div>

This is my js code:这是我的js代码:

AUTHORIZATION_TYPES = {
  1 : "None",
  2 : "Basic",
  3 : "Bearer",
};

You can use ng-if which can render elements selectively.您可以使用ng-if可以有选择地呈现元素。

The variable filter.environment will be changed as the user select options.变量filter.environment将更改为用户 select 选项。 It will hold the key of your object.它将保存您的 object 的key So, in the markup:因此,在标记中:

<div ng-if="filter.environment === 1" class="boxone"></div>

You can use any of the ng-show/ng-hide or ng-if directives, to achieve that.您可以使用任何 ng-show/ng-hide 或 ng-if 指令来实现这一点。

<div class="boxone"></div>
<div ng-show="filter.environment == 1" class="boxtwo"></div>
<div ng-show="filter.environment == 2" class="boxthree"></div>
<div ng-show="filter.environment == 3" class="boxfour"></div>

ng-if removes html element whereas ng-show/ng-hide just hides them, you will have to choose one according to your needs. ng-if 删除 html 元素,而 ng-show/ng-hide 只是隐藏它们,您必须根据需要选择一个。

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

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