简体   繁体   English

角度指令中的单选按钮不起作用

[英]Radio buttons in angular directive not working

I'm trying to make an angular directive that takes in an object with a question and a number of answers.我正在尝试制作一个角度指令,该指令接收一个带有问题和多个答案的对象。 Then it should show the answers as radio buttons so the user can select one to vote for, then it could be sent back to the server.然后它应该将答案显示为单选按钮,以便用户可以选择一个投票,然后可以将其发送回服务器。

On my version the ng-model/scope variable isn't updating.在我的版本中,ng-model/scope 变量没有更新。

<div>
  <h3> {{poll.question}}</h3>
  <div class="list-group">
<form>
  <label ng-repeat="option in poll.options" for="{{option.optionName}}">{{option.optionName}}
    <input type="radio" id="{{option.optionName}}" ng-model="selectedOption" ng-value="option.optionName" name="option"/>
  </label>
</form>
 </div>
   <button ng-class="btn" ng-click="sendOption()">Send Vote</button>
  <p>the option you selected is: {{selectedOption}}</p>

  .directive('voter', function () {
return {
  templateUrl: 'app/voter/voter.html',
  restrict: 'EA',
  scope:{
    poll:'='
  },
  controller:function($scope,$http,Auth){
      $scope.selectedOption = 'no option selected';
      $scope.sendOption = function(){console.log($scope.selectedOption);};


  },
  link: function (scope, element, attrs) {
  }
};
})

It displays the options for the poll answers but $scope.selectedOption doesn't change?它显示投票答案的选项,但 $scope.selectedOption 没有改变? I've not used radio buttons on Angular before so probably missed something obvious.我之前没有在 Angular 上使用过单选按钮,所以可能错过了一些明显的东西。

Thank for any help感谢任何帮助

You can use this method to keep track of the selectedOption assuming that you only have one selectedOption at a time.假设您一次只有一个 selectedOption,您可以使用此方法来跟踪 selectedOption。 It initializes the selectedOption variable on the form and then each time you click an input, it tells the parent form to change the variable to the selected index.它初始化表单上的 selectedOption 变量,然后每次单击输入时,它都会告诉父表单将变量更改为所选索引。

<form ng-init="selectedOption=0">
  <label ng-repeat="option in poll.options" for="{{option.optionName}}">{{option.optionName}}
    <input type="radio" id="{{option.optionName}}" ng-value="option.optionName" ng-click="$parent.selectedOption=$index" name="option"/>
  </label>
</form>

The problem is that you are using ng-model inside ng-repeat .问题是您在ng-repeat中使用了ng-model When you use ng-repeat each item in the repeater has its own scope created.当您使用ng-repeat时,转发器中的每个项目都会创建自己的范围。 When you click on a radio button, you update selectedOption on this newly created scope ... not the scope on the directive.当您单击单选按钮时,您会在这个新创建的范围上更新selectedOption ......而不是指令上的范围。 This is why the binding in your paragraph isn't being updated.这就是您段落中的绑定未更新的原因。

You can quickly fix this by using an object ( vote ) to hold the voting result:您可以通过使用对象 ( vote ) 来保存投票结果来快速解决此问题:

<input type="radio" id="{{option.optionName}}" ng-model="vote.result" ng-value="option.optionName" />
...
<p>the option you selected is: {{vote.result}}</p>
...
controller:function($scope) {
  $scope.vote = {result: null}
  $scope.sendOption = function(){console.log($scope.vote.result);};
}

See this plunker .看到这个plunker

Edit: I fixed a small bug.编辑:我修复了一个小错误。 It was accessing vote via undefined.它正在通过 undefined 访问投票。 Instead of square brackets, my answer now uses a dot.我的答案现在使用点代替方括号。 Plunker has also been updated. Plunker 也已更新。

For more information see有关更多信息,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM