2017-10-18 39 views
0

我想从list1更新list2,并且我使用了ng-change。在选择不工作ng-change

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

,当我从我的第一个列表1选择elemt方法updateList2从未invocked

<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); 
} 
+0

控制台中的错误?该应用程序可以工作吗? –

+0

请上传一个[Plunker](https://plnkr.co)... –

+0

你不需要传递一个参数给'updateList2'函数。 '$ scope.updateList2 = function(){alert($ scope.idLIST1)}' – Zooly

回答

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); 
      } 
     } 
     ]);