根据另一个字段选择获取新值后更新下拉列表内容

Update the dropdown contents after GET new value based on another field selection

本文关键字:更新 下拉列表 新值 获取 另一个 字段 选择      更新时间:2024-04-17

我有一个View,其中有2个md自动完成字段绑定到值列表。

当我选择第一个字段时,我会发送一个GET请求,以获取要绑定到下一个字段的值列表。

我得到了新列表,并将我的第二个控件的值更新为新列表。

但是,在UI中,第二个列表的值内容没有被更新以显示新的列表值。

第二个控件未使用新值刷新。

我试过$scope$apply()。

但是,它不起作用。我的开发工具崩溃了。

function CompanyInfoCtrl($state, $scope, genericInfo) {
  this.states = genericInfo.states;
  this.cities = genericInfo.cities;
      
  //when user Selects a State
  //Update this.cities with the new ListOfCities in the selected State
  //Call this method where field = this.cities
  function(address, field) {
    if(address && address.state) 
      return $http.get('http://localhost:8090/common/listofcitiesbystate/' + address.state).then(function(response){
        field = response.data;
      })
  }
}

模型没有设置为Dirty状态,因此没有用新值刷新。

在我的代码中,我之前将新值设置为:-

//where UiElementArrayList is a list of object and is bind to md autocomplete
//I need to update the list with new valueList based on User Action/Selection
//GET call to fetch new data
    this.cities =  response.data   

这解决了问题:-

//1. Empty the array
//2. Push the elements from the Response array to the UiElementArrayList
//i.e.
while(this.cities.length > 0)
    this.cities.pop()
for(i=0; i < response.data.length; i++)
    this.cities.push(response.data[i])

这将模型设置为脏状态并进行刷新,我可以看到新的List元素。:)

如果你遇到同样的问题,请提出任何改进/更好的方法。