先定一個變量保存id和name,然后循環出來數據添加到該變量中,修改的時候就是比較當前這條數據的id是否跟循環出來的數據相等。
html部分:
<select ng-model="trains.name" class="form-control" ng-options="item.name for item in scenedata" ng-change="selectTastList(mytaskTypes)" style="width: 130px;"> <option value="">-- 請選擇 --</option> </select>
js部分:
//查詢 $scope.seceneQuery = function () { $scope.scenedata = []; interfaceService.sceneQuery().then(function (response) { if (response.data.success == false) { toaster.pop('error', 'Error', response.data.message); return false; } angular.forEach(response.data.data, function (item) { $scope.scenedata.push({ 'id': item.id, 'name': item.name }); }) $scope.trains.name = $scope.scenedata[0].id; console.log($scope.scenedata); $scope.selectTastList(); }, function (error) { $scope.toaster.text = 'Network error. Please try again later!'; toaster.pop('error', 'Error', $scope.toaster.text); }) }
修改數據時select所選中的值的時候:
$scope.openUpdateModel = function (item) { $scope.scenedata = []; var modalInstance = $modal.open({ templateUrl: 'modelu.html', controller: 'ModaluCtrl', scope: $scope }); $scope.seceneQuery(); interfaceService.sceneQuery().then(function (response) { if (response.data.success == false) { toaster.pop('error', 'Error', response.data.message); return false; } angular.forEach(response.data.data, function (i) { //這里就是比較他們是否相等 if (i.id == item.sceneid) { $scope.trains.name = i.id; } }) }, function (error) { $scope.toaster.text = 'Network error. Please try again later!'; toaster.pop('error', 'Error', $scope.toaster.text); }) };