3

I need to validate my radio button field using Angular.js .Let me explain with my code below.

<form name="myForm"  enctype="multipart/form-data" novalidate>
<div>
<input type="radio"  ng-model="new" value="true" ng-required="!new"> new 
<input type="radio"  ng-model="new" value="false" ng-required="!new">Old
</div> 
<input type="button" class="btn btn-success" ng-click="addData(myForm);"  value="Add" ng-show="myForm.$valid"/>
</form>

Here i need when user is not selecting any radio button the add button should not be visible to user.I did some coding but its not happening like that .Please help me.

3
  • Both inputs should be binded with same model.. Commented Dec 31, 2015 at 7:20
  • i did as per you but still issue is there . Commented Dec 31, 2015 at 7:21
  • Working example: plnkr.co/edit/hE2hVVmXsw6mCzcHZGqw?p=preview Commented Dec 31, 2015 at 7:23

1 Answer 1

1

var app = angular.module('myApp', []);


app.controller('MyCtrl', ['$scope', function ($scope) {
    $scope.people = [{
        name: "John"
    }, {
        name: "Paul"
    }, {
        name: "George"
    }, {
        name: "Ringo"
    }];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form name="myForm" ng-app="myApp" ng-controller="MyCtrl">
    <p>Favorite Beatle</p>
    <ul>
        <li ng-repeat="person in people">
            <label>{{person.name}}
                <input type="radio" ng-model="$parent.name" name="name" value="{{person.name}}" required />
            </label>
        </li>
    </ul>
    <p><tt>myForm.$invalid: {{myForm.$invalid}}</tt></p>
    <button ng-disabled="myForm.$invalid">Submit</button>
</form>

Sign up to request clarification or add additional context in comments.

1 Comment

What does $parent do ? Will you mind explaining it a bit ? OP had static radio buttons, what is the point of using array and binding it in ng-repeat ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.