如何在angularjs下拉元素上正确使用ng-change? [英] How to use properly ng-change on angularjs dropdown element?

查看:328
本文介绍了如何在angularjs下拉元素上正确使用ng-change?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模板中有这个下拉列表:

I have this dropdownlist in my template:

    <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
        <div class="input-group input-group-sm">
            <select class="form-control" 
                    data-ng-options="o.name for o in list.itemsStatus" 
                    data-ng-model="list.currentItemsStatus"
                    ng-change="list.getItems(o.id)">   
            </select>
        </div>
    </div>

以下是控制器中定义itemsStatus的方式:

Here is how is defined itemsStatus in controller:

  this.itemsStatus = [{ name: "all", id: 1 }, { name: "new", id: 2 }, { name: "toUpdate", id: 3 }];

选择新项目时我启动此功能:

when new item is selected I fire this function:

    function getItems(status) {
        switch (status) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            default:
        }
    }

但是传递参数 - status 始终未定义。

But passed parameter- status is always undefined.

如何将所选项作为参数传递给getItems()事件?

How can I pass to getItems() event the selected item as parameter?

推荐答案

ng-change 事件无法通过 ng -options 当前项目为值。

ng-change event can't pass ng-options current item as value.

您已经在 ng-model 中选择了值,因此您可以传递已经 o (当前选定的值)

You already do have selected value inside your ng-model, so you can pass your model which already has o(current selected value)

ng-change="list.getItems(list.currentItemsStatus.id)"    

这篇关于如何在angularjs下拉元素上正确使用ng-change?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆