角JS:得到NG-变化NG-模型

问题描述:

我有以下HTML

<select ng-model="country" ng-options="c.name for c in countries" ng-change="filterByCountry"></select>

这是beeing由下列对象喂国家名单

That is beeing fed by the following object with a list of countries

$scope.countries = [{name:Afeganistão, country:AF}, {name:África do Sul, country:ZA}, name:Albânia, country:AL}, {name:Alemanha, country:DE}, {name:Andorra, country:AD} ...];

当我改变我的下拉值,我期待我的模型($ scope.country)进行更新,filterByCountry函数内部,但事实并非如此。缺少什么我在这里?

When I change my dropdown value I was expecting my model ($scope.country) to be updated, inside filterByCountry function, but it isn't. What am I missing here?

NG-变化处理程序的 NG-模型前发射code>实际上被更新。如果你想 filterByCountry 将每次发射 $ scope.country 的变化(而不仅仅是当下拉变化)你应该改用以下内容:

The ng-change handler is fired before the ng-model is actually updated. If you want filterByCountry to be fired every time $scope.country changes (rather than just when the dropdown changes), you should use the following instead:

$scope.$watch('country', filterByCountry);

我总是觉得它更有用在我的 $范围变化作出反应,而不是DOM事件时可能的。

I always find it more useful to react to changes in my $scope rather than DOM events when possible.