2017-06-06 5 views
1

配列に基づいてラジオセレクタを設定し、その値の代わりにキーをサーバに返そうとしています。ここでng-valueの代わりにng-modelのキーを取得できますか?

は配列です。ここで

$scope.customer_types = ['a', 'b', 'c', 'd', 'other']; 

は、無線セレクタです:私が選択した場合選択した顧客のタイプを格納し、例えば、数を返す

<label style="margin-right:15px"><input type="radio" ng-model="ctype" ng-value="customer_type" name="customer_type">{{customer_type}}</label> 

今$ scope.ctypeますotherサーバーに4を送信する必要があります。あなたはng-repeatにキーと値を分離する必要があります

答えて

1

ng-repeat="(customer_key,customer_type) in customer_types"

...私は、このために逆フィルタを行うことができますが、私はそれが非常に簡単になりng-value="customer_type[key]"のようなものを使用することができるかどうかは私には思えます。

0

「customer_typesで(キー、CUSTOMER_TYPE)」使用NGリピート=とラジオボタンの変更など:

<label style="margin-right:15px"><input type="radio" ng-model="ctype" ng-value="key" name="customer_type">{{customer_type}}</label> 
1

キーが配列の要素の位置になりたいので、あなたが使用することができます$indexをクリックして、選択した顧客タイプを取得します。

function MyCtrl($scope) { 
 
    $scope.customer_types = ['a', 'b', 'c', 'd', 'other']; 
 
    $scope.selectedType = function(index) { 
 
    $scope.ctype = index; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<body ng-app ng-controller="MyCtrl"> 
 
    <div ng-repeat="customer_type in customer_types"> 
 
    <label style="margin-right:15px"> 
 
    <input type="radio" ng-value="$index" name="customer_type" ng-model="ctype" ng-click="selectedType($index)">{{customer_type}}</label> 
 
    </div> 
 

 
    Selected Customer type : {{ctype}} 
 
</body>

関連する問題