-->

How to use properly ng-change on angularjs dropdow

2019-03-01 00:52发布

问题:

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