将多个随机单选按钮值发送到AngularJS中的控制器

时间:2016-05-05 15:01:31

标签: javascript jquery angularjs json

我使用Monaca,Onsen UI和AngularJS构建了一个跨平台应用程序。

在一个页面上,我有一个项目列表,每个项目都有一个单选按钮列表,如下所示在 view.html

中创建
<ul class="list">
    <li class="list__item" ng-repeat="checkItemDescription in data">
        <span class="list__item__line-height"><strong>{{checkItemDescription.checkitemdesc}}</strong></span>

        <label class="radio-button" ng-repeat="option in checkItemDescription.options">
            <input type="radio" 
                name="option_question_{{option.fleetcheckitemid}}" 
                ng-model="option.fleetcheckid">
            <div class="radio-button__checkmark"></div>
            {{option.checkvaluedesc}}
        </label>
    </li>
</ul>

列表根据需要构建和显示,用户可以点击和单选按钮选择它们。

当用户选择任何项目上的任何单选按钮时,我需要同时保存&#34; checkItemDescription &#34; ID以及&#34; option.fleetcheckid &#34;到JSON对象。我的项目列表可以是任意长度,我需要将每个列表项作为JSON对象发送到我的数据库。

如何向bot发送&#34; checkItemDescription &#34; ID以及&#34; option.fleetcheckid &#34;当我的ng-model只包含&#34; option.fleetcheckid &#34;

的值时

1 个答案:

答案 0 :(得分:0)

如果目的是使用一次,您可以使用 ng-hide 并传递变量$scope.InputSet然后 ng-change ,被调用的函数将$scope.InputSet设置为true以及您需要的逻辑。

<ul class="list" ng-hide="inputSet">
    <li class="list__item" ng-repeat="checkItemDescription in data">
        <span class="list__item__line-height"><strong>{{checkItemDescription.checkitemdesc}}</strong></span>

        <label class="radio-button" ng-repeat="option in checkItemDescription.options">
            <input type="radio" 
                name="option_question_{{option.fleetcheckitemid}}" 
                ng-model="option.fleetcheckid" 
                ng-change="funcToCall(checkItemDescription.id, option.fleetcheckitemid)">
            <div class="radio-button__checkmark"></div>
            {{option.checkvaluedesc}}
        </label>
    </li>
</ul>

控制器

$scope.funcToCall = function(checkItemDescription.id, option.fleetcheckitemid){
  $scope.inputSet = true;
  //Your logic...
};

如果您只想删除该复选框,则必须通过该选项并将其删除

$scope.funcToCall = function(checkItemDescription.id, option){
  // no need to use ng-hide here
  $scope.optionId = option.id;
    //remove the option
  delete option;
    //Your logic...
};

这应该有用。