angularJs里单选框radio怎么使用ng-model?

xiaoxiao2021-02-28  92

使用ng-model和ng-value

ng-mode是当前选中的值,, ng-value是这个radio的值。

使用ng-model把radio绑到一个变量上,ng-value使用表达式来表示值。选中时它的值就是ng-value的值了。测试代码如下:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>radio的绑定</title> <script src="angular.js"></script> </head> <body ng-app="app"> <div ng-controller="appCon"> <form ng-submit="ok()"> <div> <input type="radio" name="a" ng-value="a" ng-model="c">11 <input type="text" ng-model="a"> <input type="radio" name="a" ng-value="b" ng-model="c">22<input type="text" ng-model="b"> </div> <h1>{{c}}<h1> <input type="submit" value="submit"> </form> </div> <script> var app = angular.module('app', []); app.controller('appCon', function($scope,$log) { $scope.ok = function(){ $log.log($scope.c); } }); </script> </body> </html>

这里可以打印下 $scope.c 会发现,它的是你选的选项了。或者直接写页面上看看。

转载请注明原文地址: https://www.6miu.com/read-74719.html

最新回复(0)