Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-model not working for radio button in AngularJS

I am new to Angular and I am trying to obtain the value of the radio button that the user has selected using ng-model. But I am not getting any output in "selected contact".

Here is My HTML

<!doctype html> <html ng-app>   <head>     <script src="http://code.angularjs.org/1.2.0rc1/angular.min.js"></script>     <script src="script.js"></script>   </head>   <body>     <form name="myForm" ng-controller="Ctrl">       <table border="0">                 <th>Contact Type</th>                 <tr ng-repeat="contact in contacttype"><td>                 <input type="radio" ng-model="contactname" name="group1" value="{{contact.name}}">{{contact.name}}                                   </td>                            </td></tr></table>       <tt>selected contact = {{contactname}}</tt><br/>      </form>   </body> </html> 

Below is my main.js

  function Ctrl($scope) {   $scope.contacttype = [                            {name: 'Both' },                            {name: 'User'},                           {name: 'Admin'}                           ]; } 

What am I doing wrong here? Not able to figure out !!!

like image 295
Shrey Shivam Avatar asked Sep 03 '13 13:09

Shrey Shivam


1 Answers

Because ng-repeat creates its own scope and what you are trying to do is assign the value to the parent scope from the child scope. So you can do

<input type="radio" ng-model="$parent.contactname" name="group1" value="{{contact.name}}"> 
like image 181
zs2020 Avatar answered Oct 14 '22 08:10

zs2020