如何在包含ng-repeat的选择上触发ng-change

Ant*_*Ant 5 angularjs angularjs-ng-repeat

我有两个选择框,第二个选择框在第一个选择框更改时自动更新.

<label>Region</label>
<select ng-model="region" 
        ng-change="clear(1)" 
        ng-options="l.region for l in locations"
></select>
<br />
<label>Country</label>
<select ng-model='country'
        ng-change="clear(2)" 
        ng-options="c.country for c in region.countries"
></select>
Run Code Online (Sandbox Code Playgroud)

我的位置是一个json对象,如下所示:

[
  {region: "Europe", countries: [{country: "France"}, {country: "UK"}/*, ...*/]},
  {region: "Africa", countries: [{country: "Cameroon"}, {country: "Algeria"}]}
  /*, ...*/
]
Run Code Online (Sandbox Code Playgroud)

这一切都很好用.

当我想将第一个选择框的值设置为Europe时,它开始变得复杂,我想在第二个选择框上触发ng-change事件.

有关如何做到这一点的任何想法?

zs2*_*020 5

您希望ng-change在第二个选择框上触发事件,这相当于region更改模型时的方案.所以你可以使用观察者来实现它.

$scope.$watch('region', function (newValue, oldValue) {
    console.log(newValue, oldValue);
})
Run Code Online (Sandbox Code Playgroud)

  • 你的oldValue和newValue已经开启,只是进行了测试 (2认同)