简体   繁体   English

在控制器中使用ng-options时如何设置选项的值?

[英]How to set the value of an option when using ng-options in controller?

I am getting customer for given id,Customer details are name, email and type of customer {0,1}. 我正在获取给定ID的客户,客户详细信息是姓名,电子邮件和客户{0,1}的类型。 For 0, customer will be Regular and 1 will Temporary. 对于0,客户将是常规客户,而1将是临时客户。

I am able to show details of customer on form but but able to select the type of customer. 我可以在表单上显示客户的详细信息,但可以选择客户的类型。 My select options are showing {'regular', 'temporary'}, but not select any option value. 我的选择选项显示{'regular','temporary'},但未选择任何选项值。

For example 例如

customer1={'name':'john', 'email':'john@gmail.com', 'customer_type':0}

Form is able to show name and email but not selecting 'regular' options 表单能够显示姓名和电子邮件,但不能选择“常规”选项

Controller 控制者

   $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 的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>

Code is working fine without any error for angularjs 1.2.23 代码正常工作,没有任何错误的angularjs 1.2.23

Just have replaced getCustomer method to object. 刚刚将getCustomer方法替换为对象。

If it is not working then Check customer object using breakpoint and check whether it's proper or not and also check which version of angularjs you are using. 如果它不起作用,则使用断点检查客户对象,并检查它是否正确,并检查您使用的是哪个版本的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> 

I think you need to specify ng-selected and set it to your current customer type. 我认为您需要指定ng-selected并将其设置为当前客户类型。 If you don't specify ng-selected you'll end up with 3 options: '', 'regular', 'temporary' 如果不指定ng-selected ,则会得到3个选项:'','regular','temporary'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM