0

我有一个用于编辑手术的 AngularJS 页面,但我无法让任何 <select> 显示当前值。表单中的所有其他字段都显示了它们的当前值,所以我不确定我做错了什么。

我有一个使用 ngResource 设置的工厂,它在我的控制器中。

// Supporting Data
SurgeryData.get({'accessToken':$rootScope.accessToken})
    .$promise.then(function(response){
        $scope.surgeryData = response.surgeryData;
    });

// Surgery Data
SurgeryServices.get({'surgeryId':$stateParams.surgeryId,'accessToken':$rootScope.accessToken})
    .$promise.then(function(response){
        $scope.surgeryDetails = response;

        $scope.formData.surgeryId = $scope.surgeryDetails.surgery.id;
        ...
        $scope.formData.surgeryStatus = $scope.surgeryDetails.surgery_status;
        ...

        $log.log( angular.toJson( $scope.formData.surgeryStatus ) );
        $log.log( angular.toJson( $scope.surgeryData.surgery_status ) );
    });

这是手术状态的 HTML

<select class="form-control" ng-model="formData.surgeryStatus" ng-options="status.name for status in surgeryData.surgery_status"></select>

这就是我从我的 2 $logs 在控制台中得到的

$scope.formData.surgeryStatus

{"id":16,"name":"Ready For Pick-Up"}

$scope.surgeryData.surgery_status

[{"id":13,"name":"Inventory Not On Site"},{"id":14,"name":"Inventory On Site"},{"id":16,"name":"Ready For Pick-Up"},{"id":15,"name":"Sterilized For Surgery"}]

表单中的 <select> 具有所有选项,但(对于本示例)未选择准备取货,并且我有一个空的?选项添加到选择

<select class="form-control ng-pristine ng-valid" ng-model="formData.surgeryStatus" ng-options="status.name for status in surgeryData.surgery_status">
    <option value="?"></option>
    <option value="0">Inventory Not On Site</option>
    <option value="1">Inventory On Site</option>
    <option value="2">Ready For Pick-Up</option>
    <option value="3">Sterilized For Surgery</option>
</select>
4

1 回答 1

2

ng-init应该是要走的路。

看法:

<select class="form-control ng-pristine ng-valid" ng-init="formData.surgeryStatus = selected" ng-model="formData.surgeryStatus" ng-options="status.name for status in surgeryData.surgery_status track by status.id">
   <option value="?"></option>
   <option value="0">Inventory Not On Site</option>
   <option value="1">Inventory On Site</option>
   <option value="2">Ready For Pick-Up</option>
   <option value="3">Sterilized For Surgery</option>
</select>

视图模型:

$scope.selected = $scope.surgeryDetails.surgery_id;

ng-init 将从您的选项中找到匹配的值并在加载时选择它。确保将您的 ng-model 设置为您想要选择的任何值,在这种情况下看起来像您的 Id?

于 2014-08-05T22:04:45.427 回答