如何正确构建AngularJS标记的无线电输入指令?

时间:2022-09-03 07:11:20

I realize that AngularJS already has an input[radio] directive and I want to leverage that as much as possible.

我意识到AngularJS已经有一个输入[radio]指令,我想尽可能地利用它。

I created a JSFiddle here, but I can't figure out how to get the ng-model property to work properly. I'm selecting each radio, but the selectedValue doesn't change.

我在这里创建了一个JSFiddle,但我无法弄清楚如何使ng-model属性正常工作。我正在选择每个收音机,但selectedValue不会改变。

Also, please tell me anything that I'm doing wrong here. I'm sure I could make some other improvements.

另外,请告诉我任何我在这里做错的事。我相信我可以做一些其他改进。

The HTML:

<div data-ng-controller="controller">
    <div
      data-ng-repeat="radio in radios"
      data-ng-model="selectedValue"
      data-name="radio1"
      data-label="{{radio.label}}"
      data-value="{{radio.value}}"
      data-labeled-radio></div>
    <br>
    selected value: {{selectedValue}}
</div>

The JavaScript:

angular.module('app', [])
.controller('controller', function($scope) {
    $scope.selectedValue = 'FOO';
    $scope.radios = [
        { label: 'foo', value: 'FOO' },
        { label: 'bar', value: 'BAR' }
    ];
})
.directive('labeledRadio', function(){
    return {
        require: ['ngModel', 'value'],
        restrict: 'A',
        replace: true,
        template: [
            '<label class="radio">',
            '  <input class="radio__input" type="radio" data-ng-model="ngModel" name="{{name}}" value="{{value}}">',
            '  <span class="radio__label">{{label}}</span>',
            '</label>'
        ].join(''),
        scope: {
            ngModel: '=',
            label: '@',
            name: '@',
            value: '@'
        }
    }
});

1 个解决方案

#1


2  

Because of the way prototypal inheritance works in JavaScript, you can't use primatives on the scope for 2-way databinding. Therefore the way to fix this is to change selectedValue to an object...

由于原型继承在JavaScript中的工作方式,您不能在范围内使用原型进行双向数据绑定。因此,解决此问题的方法是将selectedValue更改为对象...

angular.module('app', [])
.controller('controller', function($scope) {
    $scope.selectedValue = { value: 'FOO' };
    $scope.radios = [
        { label: 'foo', value: 'FOO' },
        { label: 'bar', value: 'BAR' }
    ];
})

<div data-ng-controller="controller">
    <div
      data-ng-repeat="radio in radios"
      data-ng-model="selectedValue.value"
      data-name="radio1"
      data-label="{{radio.label}}"
      data-value="{{radio.value}}"
      data-labeled-radio></div>
    <br>
    selected value: {{selectedValue.value}}
</div>

Fiddle: http://jsfiddle.net/gdnKW/

For a full explanation, see here: https://github.com/angular/angular.js/wiki/Understanding-Scopes

有关完整说明,请参阅此处:https://github.com/angular/angular.js/wiki/Understanding-Scopes

#1


2  

Because of the way prototypal inheritance works in JavaScript, you can't use primatives on the scope for 2-way databinding. Therefore the way to fix this is to change selectedValue to an object...

由于原型继承在JavaScript中的工作方式,您不能在范围内使用原型进行双向数据绑定。因此,解决此问题的方法是将selectedValue更改为对象...

angular.module('app', [])
.controller('controller', function($scope) {
    $scope.selectedValue = { value: 'FOO' };
    $scope.radios = [
        { label: 'foo', value: 'FOO' },
        { label: 'bar', value: 'BAR' }
    ];
})

<div data-ng-controller="controller">
    <div
      data-ng-repeat="radio in radios"
      data-ng-model="selectedValue.value"
      data-name="radio1"
      data-label="{{radio.label}}"
      data-value="{{radio.value}}"
      data-labeled-radio></div>
    <br>
    selected value: {{selectedValue.value}}
</div>

Fiddle: http://jsfiddle.net/gdnKW/

For a full explanation, see here: https://github.com/angular/angular.js/wiki/Understanding-Scopes

有关完整说明,请参阅此处:https://github.com/angular/angular.js/wiki/Understanding-Scopes