ng-change选择不起作用

时间:2017-10-18 14:50:35

标签: javascript angularjs

我正在尝试从list1更新list2,并且我使用了ng-change。

我的应用已完全配置,并且html页面的控制器设置得很好。

当我从第一个list1中选择elemt时,方法updateList2永远不会重新启动

<label>NameLIst1</label>
<select ng-model="idLIST1" 
        ng-change="updateList2(idLIST1)"
        ng-options="elt.id as elt.name for elt in list1">

        <option  value="">select one</option>
</select>

<label>Name LIST 2</label>
<select  ng-model="idLIST2"                 
        ng-options="elt.id as id.name for elt in list2">

        <option  value="">select one</option>
</select>


$scope.updateList2 = function(id){
alert("id ="+id);
}

1 个答案:

答案 0 :(得分:1)

我有下面写的代码,试试这个 它的工作正常,也适用于param, 但你不需要在方法中传递参数

<div class="container" ng-controller="TestController">
    <label>NameLIst1</label>
    <select ng-model="idLIST1"
            ng-change="updateList2(idLIST1)"
            ng-options="elt.id as elt.name for elt in list1">

        <option value="">select one</option>
    </select>

    <label>Name LIST 2</label>
    <select ng-model="idLIST2"
            ng-options="elt.id as id.name for elt in list2">

        <option value="">select one</option>
    </select>
</div>

var app = angular.module("test", ["ngRoute"])
        .controller("TestController", ['$scope', function ($scope) {
            $scope.list1 = [
               { id: 1, name: 'data1' },
               { id: 2, name: 'data2' },
               { id: 3, name: 'data3' },
               { id: 4, name: 'data4' },
            ];
            $scope.updateList2 = function (id) {
                alert("id =" + id);
            }
        }
        ]);