Input radio ng-model not updated after all
angularjs, javascript, radio-button
Solution
ALWAYS put a dot in `ng-model`
You are currently trying to pass a primitive to a nested scope which will break 2 way binding. If you pass an object however it will maintain reference to the original object
Simply changing :
$scope.radioVal = "nothing changes me";
To:
$scope.myModel={radioVal : "nothing changes me"};
And using the dot in `ng-model`
<input ng-model="myModel.radioVal">
changes the inheritance and therefore the binding.
DEMO
Problem
I try to solve a puzzle why an ng-model of input[radio] controls is not set as I expect. When a page is loaded all radios are initiated correctly. Clicking on different controls changes the radioVal variable - its value is rendered in the page. The problem is that it looks like it takes place only in a DOM. When I debug the code the `$scope.radioVal` is always the same ... The isolated scope of `modalDialog` does not contain the `radioVal` property. In which scope the radio `ng-model` is created ? Is it a different instance of it? The working code can be found here on jsfiddle. My html code is: ``` <div ng-app = "app"> <div ng-controller="mainCtrl"> <a ng-click="showModalDlg()">Click me</a> <br/><br/> <modal-dialog show="showModal" action="actionFun"> <form> <input type="radio" ng-model="radioVal" value="1">One<br/> <input type="radio" ng-model="radioVal" value="nothing changes me">Two<br/> <input type="radio" ng-model="radioVal" value="3">Three<br/> <br/><br/> The model changes in the DOM as expected: <b>radioVal = {{radioVal | json}}</b> <br/> but by pressing the Action button you can see that the model has not been modified. </form> <a class="button" action>Action</a> </modal-dialog> </div> </div> ``` My angular code: ``` angular.module('common', []) .controller('mainCtrl', ['$scope', function($scope){ $scope.showModal = false; $scope.radioVal = "nothing changes me"; $scope.showModalDlg = function() { $scope.showModal = !$scope.showModal; }; $scope.actionFun = function() { console.log('actionFun ...' + $scope.radioVal); }; }]).directive('modalDialog', function () { return { restrict: 'E', scope: { show: '=', action: '&', }, replace: true, transclude: true, link: function (scope, element, attrs) { scope.hideModal = function () { scope.show = false; scope.$apply(); }; $('a[hide]', element).on('click', function(){ scope.hideModal(); }); $('a[action]', element).on('click', function(){ console.log('There is no radioVal in isolated scope either ... ' + scope.radioVal); scope.action()(); scope.hideModal(); }); }, template: '<div class=\'ng-modal\' ng-show=\'show\'><div class=\'ng-modal-overlay\'></div><div class=\'ng-modal-dialog\' ng-style=\'dialogStyle\'><div class=\'ng-modal-dialog-content\' ng-transclude></div></div></div>' } }); angular.module('app', ['common']) ```