2015-10-17 56 views
0

我正在为给定ID获取客户,客户详细信息是名称,电子邮件和客户类型{0,1}。 对于0,客户将为Regular,1将为Temporary。如何在控制器中使用ng-options时设置选项的值?

我能够在窗体上显示客户的详细信息,但能够选择客户类型。我的选择选项显示{'regular','temporary'},但不选择任何选项值。

例如

customer1={'name':'john', 'email':'[email protected]', '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> 
+0

你*已经*获得期权的价值;也许你问的是如何获得物品的“索引”?即现在,'customer.type'可以等于* values *''regular''或''temporary'',但是你的模型有'0'和'1'。 – Claies

+0

@Claies,是的,你是对的.. – geeks

回答

1

代码是没有任何错误的工作罚款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': '[email protected]', 
 
    '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>

0

我认为您需要指定ng-selected并将其设置为您当前的客户类型。 如果不指定ng-selected你会选择3结束了:“”,“普通”,“临时”

相关问题