[英]how to set the value of an option when using ng-options in a select with angularjs
[英]How to set the value of an option when using ng-options in controller?
我正在获取给定ID的客户,客户详细信息是姓名,电子邮件和客户{0,1}的类型。 对于0,客户将是常规客户,而1将是临时客户。
我可以在表单上显示客户的详细信息,但可以选择客户的类型。 我的选择选项显示{'regular','temporary'},但未选择任何选项值。
例如
customer1={'name':'john', 'email':'john@gmail.com', 'customer_type':0}
表单能够显示姓名和电子邮件,但不能选择“常规”选项
控制者
$scope.customers_type=['regular','temporary'];
//I will get details of customer
$scope.customer=getCustomer($scope.id);
if($scope.customer.customer_type ==0)
$scope.customer.type=$scope.customers_type[0]
else
$scope.customer.type=$scope.customers_type[1]
的HTML
<div>
<label for="Name">Name </label>
<input ng-model='customer.name' name="Name" type="text">
</div>
<div>
<label for="email">Email </label>
<input ng-model='customer.email' name="email" type="text">
</div>
<div>
<label for="enterprise">Type of type of Customer </label>
<select ng-model='customer.type' type="text" ng-options="type for type in customers_type">
</select>
</div>
代码正常工作,没有任何错误的angularjs 1.2.23
刚刚将getCustomer
方法替换为对象。
如果它不起作用,则使用断点检查客户对象,并检查它是否正确,并检查您使用的是哪个版本的angularjs
。
angular.module("myApp", []).controller('MyContrl', function($scope) { $scope.customers_type = ['regular', 'temporary']; //I will get details of customer $scope.customer = { 'name': 'john', 'email': 'john@gmail.com', 'customer_type': 0 }; if ($scope.customer.customer_type === 0) { $scope.customer.type = $scope.customers_type[0] } else { $scope.customer.type = $scope.customers_type[1] } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="MyContrl"> <div> <label for="Name">Name</label> <input ng-model='customer.name' name="Name" type="text"> </div> <div> <label for="email">Email</label> <input ng-model='customer.email' name="email" type="text"> </div> <div> <label for="enterprise">Type of type of Customer</label> <select ng-model='customer.type' type="text" ng-options="type for type in customers_type"> </select> </div> </div>
我认为您需要指定ng-selected
并将其设置为当前客户类型。 如果不指定ng-selected
,则会得到3个选项:'','regular','temporary'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.