如果点击了按钮,则应在ng-repeat中选中第一个复选框

时间:2017-09-06 08:42:41

标签: javascript angularjs checkbox angularjs-ng-repeat

大家好我刚接触到angularjs,有人可以帮我吗?当我点击按钮时,我需要的所有内容都是要检查的ng-repeat列表中的第一个复选框。我做错了吗?这是我的代码:

复选框

<fieldset class="top5">
    <legend title="Categories"><span class="info blackLabelUpper"><b>Dish Categories</b><span class="TextCtrlReqBgImage">&nbsp;&nbsp;</span></span></legend>
    <span class="checkbox-list" ng-repeat="dishType in dishTypes">
        <input type='checkbox'  ng-click="toggleDishType(dishType)" ng-model="dishType.checked" value="{{dishType}}" ng-checked="selectedDish.Type.indexOf(dishType) > -1">
        {{dishType}}<br />
    </span>
</fieldset>

按钮

<kendo-button id="btnNewDish" class="k-primary" ng-click="onNewDishClick(true)">
    <i class="fa fa-plus fa-lg" aria-hidden="true"></i>New Dish
</kendo-button>

功能

scope.onNewDishClick = function (showNewDIshMessage) {
    scope.dishTypes[0].dishType = true;
}

3 个答案:

答案 0 :(得分:0)

我认为功能定义应该是这样的:

scope.onNewDishClick = function (showNewDIshMessage) {
 scope.dishTypes[0].checked = true;
 }

答案 1 :(得分:0)

<div ng-repeat="restorent_type in restaurant_types" style="margin:5px;" class="col col-offset-10">
    <label class="item item-radio radio-label {{restorent_type.RESCOD}}">
        <input type="checkbox" name="group" ng-model="restorent.types" value="{{restorent_type.RESCOD}}" ng-click="filters.RESCOD = restorent_type.RESCOD" ng-checked="$first">       
        <div class="item-content">
            {{restorent_type.RESCOD}}
        </div>
        <i class="radio-icon ion-checkmark"></i>
    </label>
</div>

或者您可以使用:

ng-model="dishType.checked" ng-init="dishType.checked=true"

答案 2 :(得分:0)

当您在输入中使用ng-model时,您不需要使用value来获取结果,因为ng-model会保存对自己的更改。

随意提问,此示例可能对您有所帮助。

&#13;
&#13;
var app = angular.module("app", []);

app.controller('ctrl', function($scope){
  $scope.items = [
    {somthing: false, title: 'a'},
    {somthing: false, title: 'b'},
    {somthing: false, title: 'c'},
    {somthing: false, title: 'd'},
    {somthing: false, title: 'e'}
  ]
  
  
  $scope.checkAll = function(){
    angular.forEach($scope.items, function(item){
      item.somthing = !item.somthing;
    });
  }
  
  $scope.checkFirstItem = function(){
      $scope.items[0].somthing = !$scope.items[0].somthing;
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <ul>
    <li ng-repeat="item in items">
      
      <input type="checkbox" ng-model="item.somthing">
      {{item.somthing}}
      {{item.title}}
    </li>
  </ul>
  
  
  <button ng-click="checkAll()">check all</button>
  <button ng-click="checkFirstItem()">check first item</button>
</div>
&#13;
&#13;
&#13;