Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 带有默认值的复选框_Angularjs_Forms - Fatal编程技术网

Angularjs 带有默认值的复选框

Angularjs 带有默认值的复选框,angularjs,forms,Angularjs,Forms,我想在init上有一个复选框。这是我的HTML: <form ng-submit="submit()" name="orderForm" novalidate> <input type="radio" id="service-professional" name="service-type" value="Professional" class="service-input" ng-model="orderForm.serviceType" ng-change="setSe

我想在init上有一个复选框。这是我的HTML:

<form ng-submit="submit()" name="orderForm" novalidate>
  <input type="radio" id="service-professional" name="service-type" value="Professional" class="service-input" ng-model="orderForm.serviceType" ng-change="setService(value)" />
  <input type="radio" id="service-premium" name="service-type" value="Premium" class="service-input" ng-model="orderForm.serviceType" ng-change="setService(value)" />
</form>
我在
$scope.orderForm.serviceType
中尝试了很多东西,但为了我的缘故,我想不出来

您能解释一下如何将Premium设置为默认选中选项吗?谢谢

试试这个:

$scope.orderForm = {
  serviceType: 'Professional'
};

以下方面应起作用:

// initializes the form
$scope.orderForm = {};

$scope.orderForm.serviceType = 'Premium';
您只需使用此->
ng checked=“您的_模型==‘您的价值’”


您需要使用ng init为复选框范围设置默认值 例如:在html中添加ng init=“scopeName=”value',然后在复选框ng model和ng checked中使用它scopeName

参见下面的代码

<form ng-submit="submit()" name="orderForm" novalidate ng-init="orderForm.serviceType="1">
    <input type="checkbox" id="service-professional" name="service-type" value="Professional" class="service-input" ng-model="orderForm.serviceType" ng-change="setService(value)" />
    <input type="checkbox" id="service-premium" name="service-type" value="Premium" class="service-input" ng-model="orderForm.serviceType" ng-change="setService(value)" />
</form>

<input type="radio" id="service-professional" name="service-type" ng-true-value="1" value="Professional" ng-false-value="0" ng-checked="orderForm.serviceType == 'Professional'" class="service-input" ng-model="orderForm.serviceType" ng-change="setService(value)" />
<input type="radio" id="service-premium" ng-true-value="1" ng-false-value="0" ng-checked="orderForm.serviceType == 'Premium'" name="service-type" value="Premium" class="service-input" ng-model="orderForm.serviceType" ng-change="setService(value)" />
<form ng-submit="submit()" name="orderForm" novalidate ng-init="orderForm.serviceType="1">
    <input type="checkbox" id="service-professional" name="service-type" value="Professional" class="service-input" ng-model="orderForm.serviceType" ng-change="setService(value)" />
    <input type="checkbox" id="service-premium" name="service-type" value="Premium" class="service-input" ng-model="orderForm.serviceType" ng-change="setService(value)" />
</form>