【问题标题】:First checkbox should be checked in ng-repeat if button clicked如果单击按钮,则应在 ng-repeat 中选中第一个复选框
【发布时间】:2018-02-14 16:05:51
【问题描述】:

大家好,我是 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;
}

【问题讨论】:

  • 试试这个&lt;input type='checkbox' ng-click="toggleDishType(dishType)" ng-model="dishType.checked" value="{{dishType.checked}}" /&gt;
  • 感谢 Ramesh 我需要在单击按钮时选中列表中的第一个复选框。我还有其他需要取消选中的角色。
  • &lt;input type='checkbox' ng-click="toggleDishType(dishType)" ng-model="dishType.dishType " value="{{dishType.dishType }}" 因为你保存在函数中的dishType 属性中
  • 谢谢 Karan 我试过这个,但我没有工作。如果我单击按钮,该操作应该会发生。我不会默认选中复选框。
  • 代替 ng-model="dishType.checked" 试试这个 ng-model="dishType"。

标签: javascript angularjs checkbox angularjs-ng-repeat


【解决方案1】:

我认为函数的定义应该是这样的:

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

【讨论】:

    【解决方案2】:
    <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"
    

    【讨论】:

    • 没问题,但是我们可以默认选中第一个复选框。
    • 是的,但逻辑应该是点击事件时。我有另一个逻辑,用户不会选择任何复选框
    【解决方案3】:

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

    请随时提出问题,此示例可能对您有所帮助。

    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;
      }
    });
    <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>

    【讨论】:

    • 我试过了,但由于某种原因它不起作用。我正在尝试开发具有 Telerik 和 angularjs 的代码并且我没有其中之一的背景的问题 ;-)
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2022-11-11
    • 1970-01-01
    相关资源
    最近更新 更多