How to use properly ng-change on angularjs dropdown element?

两盒软妹~` 提交于 2019-12-20 04:19:42

问题


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>

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:
        }
    }

But passed parameter- status is always undefined.

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


回答1:


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

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)"    


来源:https://stackoverflow.com/questions/42768674/how-to-use-properly-ng-change-on-angularjs-dropdown-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!