如何从点击保存按钮的“选择框”中选择“更新”新值?

时间:2016-06-11 13:14:05

标签: javascript angularjs

如何updateselect box点击保存按钮选择新值。

我在我的JS函数中使用这样的ng-click函数进行更新按钮:

   $scope.updateDealDetail = function updateDealDetail (){
      $scope.showEditView = !$scope.showEditView;
      $scope.dealDetail.decisionMakerDetail.email = $scope.selectedid;
   }

我的编辑按钮功能:

$scope.editUserDetail = function editUserDetail(){
        $scope.showEditView = !$scope.showEditView;
        $scope.showSubmitView = !$scope.showSubmitView;
        deal.getIdData($scope.accountDetail. accountUsers[0].role,$scope.accountDetail.id).then(function successCb(data){
          $scope.editIdOptionsData=data;
          $scope.selectedid = $scope.editIdOptionsData[0].email;
      });
    };

我的bitton点击的HTML是这样的:

<select ng-model="selectedid" class="form-control">
   <option ng-selected="selectedid" ng-repeat="eiod in editIdOptionsData" value="{{eiod.email}}">{{eiod.email}}

    

<button ng-click="updateDealDetail(eoid.email)" ng-disabled="dealDataSaveButtonDisabled">Update</button> 

我正在通过ng-repeat尝试这一点,因为通过使用ng-options,我的数据通过API现在显示在框中。但是我的第一个索引上的数据只是设置了。如何设置选择框的默认值并选择任何值,onclick需要更新该值。

2 个答案:

答案 0 :(得分:1)

不要使用ngRepeat渲染选项,这是你的问题。正确的代码是:

<select class="form-control" 
        ng-model="selectedid" 
        ng-options="eiod.email as eiod.email for eiod in editIdOptionsData">
</select>

答案 1 :(得分:0)

ng-options元素上使用ng-modelng-change<select>的最佳做法

在线演示 - https://plnkr.co/edit/Gaa8sMgerRv6chio4iYa?p=preview

HTML

<select 
    ng-model="selectedItem" 
    ng-change="onSelectedItemChanged()"
    ng-options="item as item.email for item in items"></select>

JS

app.controller('MainCtrl', function($scope) {
  $scope.items = [{
    email: 'asd1@asd.com'
  }, {
    email: 'asd2@asd.com'
  }];

  $scope.selectedItem = $scope.items[0];

  $scope.onSelectedItemChanged = function() {
    alert('you changed it to ' + $scope.selectedItem.email);
  }
});