在我的表单中,Angular 正在观察选择框的变化,然后使用所选数据加载数据以供以后选择框使用。这工作正常,除了观察者的初始化,Angular 不会使用选定的选项(由服务器提供)保留现有数据并立即加载新数据。
<form action="join" method="post" ng-controller="JoinController">
...
<select name="state" ng-model="state">
<option value="">Select...</option>
...data provided on initial load...
</select>
<select name="institution_id" ng-model="institutionId" ng-options="option.id as option.name for option in institutionOptions">
<option value="">{{ defaultInstitutionOption }}</option>
...data provided on initial load...
</select>
...
</form>
还有 Angular 控制器...
app.controller('JoinController', function($scope, $http) {
$scope.defaultInstitutionOption = "Select state...";
$scope.$watch('state', function() {
$scope.defaultInstitutionOption = "Loading institutions...";
$http.get('institutions?state=' + encodeURIComponent($scope.state)).success(function(institutions) {
$scope.institutionOptions = institutions;
$scope.defaultInstitutionOption = "Select...";
});
}, true);
}