javascript - 如何使用 Angular.js 验证输入类型单选按钮

标签 javascript angularjs

我需要使用 Angular.js 验证我的单选按钮字段。让我用下面的代码解释一下。

<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>

这里我需要当用户没有选择任何单选按钮时用户不应该看到添加按钮。我做了一些编码但它不是那样发生的。请帮助我。

最佳答案

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>

关于javascript - 如何使用 Angular.js 验证输入类型单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34542727/

相关文章:

javascript - angularJS模块声明中空数组的含义

angularjs - 哪个客户端框架(angularjs + oauth2)与identityserver3一起使用

javascript - AngularJS Material $mdSticky 不工作

javascript - Angular 模板中的值未扩展

javascript - 通过.htaccess Node.js Express隐藏端口号

javascript - 如何在函数中为 this.setattribute 传递参数?

javascript - 当日期来自不同时区时,在 IOS 中为 angularjs 项目返回无效日期

javascript - 如何在不重新加载页眉/页脚或左导航的情况下加载右 Pane

angularjs - Angular UI-Routing,页面刷新时自动重定向到父状态

javascript - 如何使用 Angular 事件处理程序引用输入元素的值