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

前端之家收集整理的这篇文章主要介绍了angularjs – 如何在包含ng-repeat的选择上触发ng-change前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个选择框,第二个选择框在第一个选择框更改时自动更新.

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

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

[
  {region: "Europe",countries: [{country: "France"},{country: "UK"}/*,...*/]},{region: "Africa",countries: [{country: "Cameroon"},{country: "Algeria"}]}
  /*,...*/
]

这一切都很好用.

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

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

解决方法

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

$scope.$watch('region',function (newValue,oldValue) {
    console.log(newValue,oldValue);
})

猜你在找的Angularjs相关文章