【问题标题】:How pick up checkbox result in post method angularjs如何选择复选框导致post方法angularjs
【发布时间】:2016-04-07 09:56:09
【问题描述】:

我有一张列出规则列表的表格。单击复选框时,我需要它们采用此“真实”值并发布到 API 端点。这已经设置好了,但我得到的是“关联规则”是未定义的。

我已经尝试在我的控制器中设置$scope.associated_rule.selected = true;,但这仍然没有定义变量并且在控制台中抛出了同样的错误。

这是我的 HTML 表单:

<form name="rules_form" method="post" ng-submit="attach()">
   <table class="table table-striped table-hover" ng-model="associated_rules">
  <thead>
   <th>Rule Types:</th>
   <th>Description:</th>
   <th>Start Time:</th>
   <th>End Time:</th>
   <th>Apply Rule to Vehicle:</th>
  </thead>
 <tr ng-repeat="associated_rule in associated_rules">
 <td>@{{ associated_rule.resource_ids.accounts }}</td>
 <td>@{{ associated_rule.description }}</td>
 <td>@{{ associated_rule.start_time }}</td>
 <td>@{{ associated_rule.end_time }}</td>
 <td><input type="checkbox" ng-model="associated_rule.selected" aria-label="rule"></td>
 </tr>
  </table>
  <button class="btn btn-primary" ng-click="attach()">Attach</button>
 </form>

我的控制器事件:

$scope.attach = function () {
        $scope.associated_rule.selected = true;
        var i;
        for (i = 0; i < $scope.associated_rule.selected.length; i++) {
            //need to create a loop where the true value is picked up and then I can send the data using a POST method. But I'm stuck on this. 
        }
        console.log(the result of the event);
    };

现在,我只想将结果保存到 console.log,因此我可以看到事件正在创建循环并显示结果。在那之后,我应该没事。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript angularjs loops


    【解决方案1】:

    我已通过将 $scope.rule 定义为空数组并将 $scope.rule.selected 默认设置为“false”来解决此问题。

    太棒了!第一步!但是,当您单击 A 复选框时,所有复选框都会被选中 - 认为这可能是 ng-repeat 导致我在背后感到痛苦。

    $scope.rule = [];
        $scope.rule.selected = false;
    

    那么,我如何确保只有我选择的复选框集而不是一次全部选中?

    【讨论】:

      【解决方案2】:

      也解决了这个问题;因为上面只是选择了整个数组;因为我没有深入研究阵列。这样做了:

      ng-model="rules.selected[associated_rule.id]" 
      

      通过在定义的数组中对规则进行建模,它会在测试时显示出来。布里尔。 :)

      【讨论】:

        【解决方案3】:

        您在单击按钮时错误地更改了复选框的值:

        $scope.associated_rule.selected = true;
        

        这将给出当前值,选择或未选择

         $log.log($scope.associated_rule.selected);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-10
          • 1970-01-01
          • 2020-09-25
          • 2018-01-30
          • 2015-07-19
          相关资源
          最近更新 更多