用angularJS獲取select數據和之前選中的select的值(修改數據時select所選中的值)


先定一個變量保存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);
              })
          };

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM