【问题标题】:How to add radio buttons and input fields using angularJS如何使用 angularJS 添加单选按钮和输入字段
【发布时间】:2015-08-31 14:48:52
【问题描述】:

我试图使用 angularJS 将输入字段和相应的单选按钮添加到我的项目中。意味着当我点击添加时,它会一起创建一个输入字段和三个相应的单选按钮,如下图所示。单击添加时,我可以成功添加输入字段。但是,当我单击添加时,在创建单选按钮时也遇到问题,输入字段的占位符应显示输入 1,再次单击添加时应显示输入 2。有人可以帮我吗。以下是我的代码。

HTML 部分:

<table class="table">
<tr class="tr_class">
    <td></td>
    <td></td>
    <td align="center"><b>functional check </b></td>
    <td align="center"><b>XXissue</b></td>
    <td align="center"><b>YY risk</b></td>
</tr>

<tr class="tr_class">
    <td class="td_class"><div class="input-group">
        <span class="input-group-addon">input 1</span>
        <input type="text" class="form-control" placeholder="input 1">
    </div> <br/></td>
    <td> </td>
    <td align="center"><input type="radio" name="a1"></td>
    <td align="center"><input type="radio" name="a1"></td>
    <td align="center"><input type="radio" name="a1"></td>
</tr>
<tr>
    <td><div class="input-group">
        <span class="input-group-addon">Check2</span>
        <input type="text" class="form-control" placeholder="input 2">
    </div> <br/></td>
    <td></td>
    <td align="center"><input type="radio" name="a2"></td>
    <td align="center"><input type="radio" name="a2" ></td>
    <td align="center"><input type="radio" name="a2" ></td>
</tr>
<tr>
    <td><div class="input-group">
        <span class="input-group-addon">input 3</span>
        <input type="text" class="form-control" placeholder="input 3">
    </div> <br/></td>
    <td></td>
    <td align="center"><input type="radio" name="a3"></td>
    <td align="center"><input type="radio" name="a3"></td>
    <td align="center"><input type="radio" name="a3"></td>
</tr>

<br><br>

<tr>

    <td><div ng-controller="AlertDemoCtrl">
        <input type="text" class="form-control" placeholder="input 3" ng-repeat="alert in alerts"
               type="{{success.type}}" close="closeAlert($index)">{{success.msg}}<br></input>
        <button type="button" class='btn btn-info' ng-click="addAlert()">+Add input</button>
        <button type="reset" class="btn btn-danger">Reset</button>
    </div> <br><br><br></td></tr>

还有我的 angularJS 部分:

{{ ngapp }}.controller('AlertDemoCtrl', function ($scope) {
  $scope.alerts = [
  ];

  $scope.addAlert = function() {
    $scope.alerts.push({msg: 'Another alert!'});
  };

  $scope.closeAlert = function(index) {
    $scope.alerts.splice(index, 1);
  };
});

【问题讨论】:

  • HTML 中单选按钮的声明在哪里?
  • 等等,现在您发布了静态单选按钮,但您的问题是关于动态按钮?实际上,您在更新中发布的那些输入框和单选按钮甚至根本没有连接到任何角度模型。
  • 是的。但我不确定如何添加动态单选按钮。这是我最关心的问题
  • 好吧,看来您是在倒退考虑。与其试图想出如何动态添加 DOM 元素,不如考虑你试图表示的数据是如何结构化的。如果你把数据搞清楚了,那么 DOM 就很容易匹配了。
  • 您尝试使用这些按钮和输入框收集什么数据?单选组与输入框有什么关系?

标签: html angularjs twitter-bootstrap


【解决方案1】:

请参阅下面附加的Plunker,我已经为您提供了有关如何修改 DOM 以便从当前视图中添加/删除元素的答案。

尽管这将详细说明如何添加元素,但删除过程是相同的。只需从元素列表中删除所需的元素。


这是动态添加单选按钮的方式。

<td ng-repeat="answer in question.answersList track by $index">
  <input type="radio" name="{{$parent.$index}}" ng-value="1" ng-model="answer.is_correct">
</td>

$scope.addRow = function() {
  $scope.questionList.push({
        "question_num": $scope.questionList.length+1,
        "answersList": [
            {
                "is_correct": "0",
                "answer_text": "Answer 1",
                "feedback": "Feedback 1"
            },
            {
                "is_correct": "0",
                "answer_text": "Answer 2",
                "feedback": "Feedback 2"
            },
            {
                "is_correct": "1",
                "answer_text": "Answer 3",
                "feedback": "Feedback 3"
            }
        ],
        "question_text": "Sample Question" + ($scope.questionList.length+1)
    });
}

【讨论】:

  • 但是返回错误如下Uncaught Error: [$injector:modulerr] Failed to instantiate module ui.bootstrap.demo due to: Error: [$injector:nomod] Module 'ui.bootstrap.demo' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
  • 将代码中不需要的行注释如下&lt;!-- &lt;script src="scripts/angular-bootstrap/ui-bootstrap-tpls.js"&gt;&lt;/script&gt; --&gt;
  • 错误不存在但它只是不起作用..无法理解为什么
  • 您能否为您的实现准备一个 plunker。也希望你确实参考了我的。
  • 我已经根据用户输入使用来自 DOM 的 Add / Remove 元素增强了答案。参考修改后的答案。
猜你喜欢
  • 2017-02-14
  • 2011-05-18
  • 2015-07-22
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
相关资源
最近更新 更多