how to auto check in ng-repeat check box when i change dropdown values

独自空忆成欢 提交于 2019-12-08 11:12:37

问题


hi all i am using angularjs ng-repeat i have checkbox and dropdown bind it now my need is when i change the dropdown values means automatically checkbox will check based on dropdown slection

 <div ng-repeat="BFMaterialStream in BFMaterialStreams">
   <input type="checkbox" ng-change="checkchange(BFMaterialStream.MaterialStream,$index)"
   ng-model="selection.ids[BFMaterialStream.MaterialStream]" name="group" id="BFMaterialStream.MaterialStream" />
    {{BFMaterialStream.MaterialStream}} 
        <select id="MaterialElevator" tabindex="7" required typeof="text" name="Elevator"
         form="DistanceMatrixFormId" class="form-control" 
          ng-model="ViewGetBUMaterialStream.ToElevator"
     >
     <option value=''>Select</option>
     <option ng-repeat="ViewGetBUMaterialStream in ViewGetBUMaterialStreams "
      value="{{ViewGetBUMaterialStream.ToElevator}}"
      >
     {{ViewGetBUMaterialStream.ToElevator}}
    </option>
     </select>
       </div>

https://jsfiddle.net/7MhLd/2546/


回答1:


Use ng-change on <select> and in the assigned function make the ng-model value of checkbox as true.

In checkbox HTML

    <input type="checkbox" ng-change="checkchange(BFMaterialStream.MaterialStream,$index)"
       ng-model="selection.ids[BFMaterialStream.MaterialStream]" name="group" id="BFMaterialStream.MaterialStream"
ng-true-value="true" ng-false-value="false"/>

In select HTML

    <select id="MaterialElevator" tabindex="7" required typeof="text" name="Elevator"
            form="DistanceMatrixFormId" class="form-control" ng-model="ViewGetBUMaterialStream.ToElevator"
            ng-change="SetCheckBoxTrue(BFMaterialStream.MaterialStream)">

in JS:

$scope.SetCheckBoxTrue= function (MaterialStream) {
   $scope.selection.ids[MaterialStream] = true;
}



回答2:


Check the fiddle for your working answer

https://jsfiddle.net/athulnair/pzuetsx1/2/

$scope.checkLine = function(index) {
    $scope.lines[index].selected = true;
  }

<div ng-repeat="line in lines">
    <input type="checkbox" ng-model="line.selected" /> {{line.text}}
    <select required typeof="text" class="form-control" ng-change="checkLine($index)" ng-model='line.text'>
      <option value='0'>Select</option>
      <option value='1'>one</option>
      <option value='2'>two</option>
      <option value='3'>three</option>
    </select>



  </div>


来源:https://stackoverflow.com/questions/43781667/how-to-auto-check-in-ng-repeat-check-box-when-i-change-dropdown-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!