I have a radio button inside a directive and I have a number of these directive on the page. All of these radio buttons have the same name. When I click on any of them a newValue in a $watch is undefined. Also ng-change event fires only ones (I guess for the same reason). My question is how can I know the current status of the radio button inside a directive on each click (that I will pass to the parent directive)?
var app = angular.module("app", []);
app.directive("choiceRb", function() {
return {
template: '<input type="radio" id="{{elid}}" name="selector" ng-change="onChange()" ng-model="checked" >',
scope: {
elid: "@"
},
link: function ($scope, $element, attr, cntr) {
$scope.checked = $element[0].querySelector("input[type=radio]").checked;
$scope.$watch('checked', function (newval, oldval) {
console.log(newval);
}, true);
$scope.onChange = function () {
console.log("onChange");
}
}
};
});
app.controller("myC", ["$scope", function ($scope) {
$scope.model = {};
$scope.model.elid = "1";
$scope.model.elid1 = "2";
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<div ng-app="app" ng-controller="myC">
<div style="width:300px; height:300px; border:1px solid red;position:relative;">
<choice-rb elid="{{model.elid}}"></choice-rb>
<choice-rb elid="{{model.elid}}"></choice-rb>
</div>
</div>