找到你要的答案

Q:How to check one of two radio button in Angular JS?

Q:如何检查一个角JS两个单选按钮?

I get data from AJAX query in Angular JS. It is information about user. There are two input radio button in page:

<input type="radio" ng-model="sex" value="1">
<input type="radio" ng-model="sex" value="2">

From AJAX I get field {'sex' : '1'}. How I can check input with value = 1 and set it value to ng-model?

I get data from AJAX query in Angular JS. It is information about user. There are two input radio button in page:

<input type="radio" ng-model="sex" value="1">
<input type="radio" ng-model="sex" value="2">

从阿贾克斯我得到场{ 'sex”:“1”}。我如何检查输入值= 1,并设置它的值NG模型?

answer1: 回答1:

Start by assigning a name to your inputs, to make them mutually exclusive:

<input type="radio" name="sex" ng-model="sex" value="1">
<input type="radio" name="sex" ng-model="sex" value="2">

Now, ng-model="sex" means that the inputs are populated based on the value of the sex property of the scope. So all you need to check the first input, from your controller, is

$scope.sex = '1';

首先给输入分配一个名称,使它们相互排斥:

<input type="radio" name="sex" ng-model="sex" value="1">
<input type="radio" name="sex" ng-model="sex" value="2">

现在,NG模型=“性”意味着输入是根据范围的性属性的值来填充的。所以你需要检查第一个输入,从你的控制器,是

$scope.sex = '1';
answer2: 回答2:

You can set that in your controller with your $scope.

<input type="radio" name="sex" ng-model="sex" value="1">
<input type="radio" name="sex" ng-model="sex" value="2">

$scope.sex = '1';

then you have set your variable for ng-model and you can use it.

您可以设置您的控制器与您的范围。

<input type="radio" name="sex" ng-model="sex" value="1">
<input type="radio" name="sex" ng-model="sex" value="2">

$scope.sex = '1';

然后为NG模型设置变量,可以使用它。

angularjs