javascript - 在 Angular 单选按钮中绑定(bind)单个 ng-model 以获得 2 ng-repeat

标签 javascript jquery angularjs radio-button

我有两个不同的列表,我需要将其显示为页面上的单个列表并在屏幕中显示为单选项目。如果未选择任何元素,或者如果用户选择默认值,则如何识别 ng-model 和选定的值。

<script>
   angular.module('valueExample', [])
     .controller('ExampleController', ['$scope', function($scope) {
       $scope.names = ['pizza', 'unicorns', 'robots'];
       $scope.my = { favorite: 'unicorns' };
     }]);
</script>
 <form ng-controller="ExampleController">
   <h2>Which is your favorite?</h2>
     <label ng-repeat="name in names" for="{{name}}">
       {{name}}
       <input type="radio"
              ng-model="my.favorite"
              ng-value="name"
              id="{{name}}"
              name="favorite">
     </label>
   <label ng-repeat="name in names2" for="{{name}}">
       {{name}}
       <input type="radio"
              ng-model="my.favorite"
              ng-value="name"
              id="{{name}}"
              name="favorite">
     </label>
   <div>You chose {{my.favorite}}</div>
 </form>

names和names2是2个列表项,我想在单个屏幕上显示

最佳答案

As I suggested, you can concat lists and use ng-repeat accordingly. Or you can use ng-repeat-start and ng-repeat-end and render accordinly.

 angular.module('valueExample', [])
   .controller('ExampleController', function($scope) {
     var names1 = ['pizza1', 'unicorns1', 'robots1'];
     var names2 = ['pizza2', 'unicorns2', 'robots2'];
     $scope.names = names1.concat(names2);
     $scope.my = {
       favorite: 'unicorns1'
     };
   });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
<form ng-app="valueExample" ng-controller="ExampleController">
  <h2>Which is your favorite?</h2>
  <div ng-repeat="name in names">
    <input type="radio" ng-model="my.favorite" ng-value="name" />
    <label>{{name}}</label>
  </div>

  <div>You chose {{my.favorite}}</div>
</form>

关于javascript - 在 Angular 单选按钮中绑定(bind)单个 ng-model 以获得 2 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42086793/

相关文章:

javascript - 在 uib-tab 中添加带有 active 和 ng-repeat 的选项卡

javascript - Kendo无法获取json文件并解析

javascript - 如何从另一个可观察对象访问一个对象

javascript - 如何显示涉及 JavaScript 变量的图像

javascript - 无法使用 jquery 操作 IE8 中的复选框

javascript - jQuery:动态更改对象的属性

javascript/jquery 模拟 anchor "tel"点击

javascript - jQuery点击事件不触发

javascript - 自定义 javascript/jquery 不适用于 Angular View

javascript - 使用 Javascript 将 mousemove 监听器添加到视频元素的控件