【发布时间】:2019-10-11 06:53:12
【问题描述】:
我正在更新一个表单,使其具有两组打开或关闭面板的单选按钮。 第一个说“是”以联系客户或“否”。如果我们要联系,请选择使用哪种方法,电话或电子邮件。 (以前只有联系与不联系的问题。)
虽然我可以让显示功能正常工作,但我不明白为什么我无法让单选按钮显示为选中状态。 (可能是我对 AngularJS 相当陌生。)
<div class="radio styled-radio" id="callTheCustomerDiv">
<input class="form-control" id="callTheCustomerRadio1"
name="callTheCustomer" type="radio" value="Yes"
ng-required="issueType=='IncidentDiv'"
ng-model="$parent.callTheCustomer"
ng-click="contactSelection('none')"/>
<label for="callTheCustomerRadio1"> Yes</label> - Please indicate how the customer wants to be contacted.<br/>
<div class="form-group" id="howToContactDiv" ng-show=state class="row fullWidth">
<div id="contactOptions" class="form-group">
<label for="phoneRadio">Phone</label>
<!-- radio indicator is not working correctly -->
<input id="phoneRadio" class="form-control" type="radio"
ng-click="contactSelection('phone')"
name="phoneSelect" ng-model="$parent.phoneSelect"
value="{{$parent.phoneSelect}}" />
<label for="emailRadio">Email</label>
<input id="emailRadio" class="form-control" type="radio"
ng-click="contactSelection('email')" name="emailSelect"
ng-model="$parent.emailSelect"
value="{{$parent.emailSelect}}" />
<br/> <!-- phoneSelect : {{phoneSelect}} -->
</div>
<div id="phoneContact" ng-show="contactType=='phone'">
<span>Phone
<input class="form-control input-sm" type="text"
name="callTheCustomerPhone"
id="callTheCustomerPhone"
ng-model="$parent.callTheCustomerPhone"
ng-required="$parent.contactType=='phone' && accForm.phoneNumber.$viewValue === ''"
maxlength="20"/>
</span>
</div>
<br/>
<div id="emailContact" ng-show="contactType=='email'">
<span>Email
<input class="form-control input-sm" type="text"
name="emailTheCustomer" id="emailTheCustomer"
ng-model="$parent.emailTheCustomer"
ng-required="$parent.contactType=='email' && accForm.emailTheCustomer.$viewValue === ''"
maxlength="150"/></span>
</div>
</div>
<input class="form-control" id="callTheCustomerRadio2"
name="callTheCustomer" type="radio" value="No"
ng-required="issueType=='IncidentDiv'"
ng-model="$parent.callTheCustomer"
ng-click="contactSelection('no')"/>
<label for="callTheCustomerRadio2">No</label>
</div>
选择电话时的样子:
和选择电子邮件时,如图所示: h2>
我将此方法添加到主控制器:
$scope.contactSelection = (function (contact) {
if (contact=='none'
||contact=='email'
||contact=='phone'){
$scope.state = true;
$scope.contactType = contact;
if (contact=='phone'){$scope.phoneSelect = true;$scope.emailSelect = false;}
if (contact=='email'){$scope.emailSelect = true;$scope.phoneSelect = false;}
}else{
$scope.state = false;
$scope.contactType = 'no';
$scope.emailTheCustomer = '';
$scope.callTheCustomerPhone = '';
$scope.phoneSelect = false;
$scope.emailSelect = false;
}
});
【问题讨论】:
-
使用
$parent是有问题的。如果模板嵌套了多个实例化子范围的指令,则双向绑定将存在数据隐藏问题。最好将ng-models绑定到控制器范围内对象的属性。 -
如果
$parent将ng-model绑定到$rootScope,控制器$scope将无法设置模型值。 -
是/否问题最好用checkboxes处理。
-
模型值被设置,只是显示没有改变。如果我按照@nash11 的建议将名称切换为 phoneOrRadio 之类的名称,则即使单击电话,收音机也会打开以接收电子邮件。我查看了其他单选按钮设置,它们使用类似的范围表示法。
-
模型正在设置,因为
ng-click处理程序正在手动设置它。如果绑定正确,ng-model控制器应该会自动设置,不需要手动设置。ngModelController需要适当的绑定才能呈现到 HTML。避免使用$parent。在 $scope 中为您的模型定义对象,然后引用该对象的属性。见Nuances of scope inheritance in AngularJS
标签: html angularjs forms radio-button