简体   繁体   English

在带有ng-options的选择下拉列表上ng-show on object array

[英]ng-show on array of objects on select dropdown with ng-options

I am using the ng-options to iterate over my array of objects and display the proper list of statuses to the view as well as bind what i need to the model. 我正在使用ng-option遍历我的对象数组,并向视图显示适当的状态列表,以及将我需要的绑定到模型。

There are two states that this view can be in at any given time and one is an empty workOrder or a workOrder that already has values. 该视图在任何给定时间都可以处于两种状态,一种是空的workOrder或已经具有值的workOrder。

Now i would like in the instance that a workOrder has returned with a status of 'A' or an 'Active' status, the 'Closed' and 'Processing' statuses will not display in the dropdown. 现在,我希望在一个工作订单返回状态为“ A”或“活动”状态的情况下,下拉菜单中不显示“已关闭”和“正在处理”状态。

I would like to use ng-show for this but would also like to know if there is a more appropriate method of going about solving this. 我想为此使用ng-show,但也想知道是否有更合适的方法来解决此问题。

my objects: 我的对象:

workOrder.statuses = [
        {
            'Status': 'Open',
            'Code': 'O',
            'Show': true
        },
        {
            'Status': 'Active',
            'Code': 'A',
            'Show': true
        },
        {
            'Status': 'Processing',
            'Code': 'P',
            'Show': true
        },
        {
            'Status': 'Closed',
            'Code': 'C',
            'Show': true
        }
    ];

my HTML on which i am using: 我正在使用的HTML:

<select title="Status" class="form-control" id="ddlStatus"
                    ng-options="status.Code as status.Status for status in ctrl.statuses"
                    ng-model="ctrl.model.Status">
</select>

I am running into issues on trying to get this to work and nothing seems to work and searching through StackOverflow i was unable to find a solid answer. 我在尝试使其工作时遇到问题,似乎没有任何效果,并且通过StackOverflow搜索时我找不到可靠的答案。

Any help is much appreciated! 任何帮助深表感谢!

First of all, you can just filter your options array like this: 首先,您可以像这样过滤选项数组:

<li ng-repeat="status.Code as status.Status for status in ctrl.statuses | filter : {Status: 'Open'}: true">

Second of all, you can populate select with the options like this 其次,您可以使用以下选项填充select

<select name="repeatSelect" id="repeatSelect" ng-model="data.repeatSelect">
  <option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name}}</option>
</select>

So you can specify there with 'ng-if' to hide the options with status you don't want to show 因此,您可以使用“ ng-if”指定此处,以隐藏您不想显示的状态的选项

Update: 更新:

So you can use filter: 因此,您可以使用过滤器:

<option ng-repeat="status in ctrl.statuses | filter : {Status: '!' + ctrl.model.Status}: true" value='{{status.Code}}'>{{status.Status}}</option>

Or if you don't want to use ng-repeat, you can just filter the options array in the scope when the selected status is changed. 或者,如果您不想使用ng-repeat,则可以在更改选定状态时在范围内过滤options数组。

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

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