0

在我的表单中,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);
}
4

1 回答 1

0

需要 2个$watch参数newValueoldValue. 对于第一个请求oldValue将是 null 或未定义。检查类似的东西

$scope.$watch('state', function(newValue,oldValue) {
//Check logic based on newValue and oldValue
}
于 2013-07-23T12:25:54.430 回答