我在 Angular js 中创建了一个自定义单选按钮组件,它将在我的应用程序中使用。以下是我为组件编写的代码。
JS
angular.module("customComponent")
.component("ngRadiobutton", {
template:
'<label class="ng-control-radio-button">' +
' <span data-ng-bind="$ctrl.label"></span>' +
' <input type="radio" name="{{$ctrl.group}}" data-ng-model="$ctrl.checked"/>' +
' <div class="ng-control-indicator-radio"></div>' +
'</label>' +
'',
bindings: {
label: '=?',
checked: '=',
group: '@'
},
controller: function () {
var $ctrl = this;
console.log($ctrl.checked); // Data is binding properly at this stage
}
});
HTML
<div data-ng-repeat="radio in vm.radioValues">
<ng-radiobutton label="radio.label " group="group1 " checked="radio.checked"></ng-radiobutton>
</div>
JSON
vm.radioValues = [{ label: 'Value1', checked: true },
{ label: 'Value2', checked:false }
];
我面临的问题是我设置的真假值没有与组件绑定。默认情况下,这两个单选按钮都未选中。谁能告诉我我的代码有什么问题。
提前致谢