2017-06-29 3 views
1

Ich kann nicht den Standardwert für Dropdown mit ng-options, kann ich Standard-Wert als Antwort, aber in Dropdown-Einstellung ist es nicht einstellen.Standardwert ist nicht für die Auswahl mit ng-init

in js

$scope.personalDetails = [ 
      { 
       'fname':'Muhammed', 
       'lname':'Shanid', 
       'email':'[email protected]', 
       'check' : 'Y', 
       'checkxDrpn' : [ 
        { key: 'Y',selectVal: "Yes"}, 
        { key: 'N',selectVal: "No"} 
       ] 
      }, 
      { 
       'fname':'John', 
       'lname':'Abraham', 
       'email':'[email protected]', 
       'check' : 'N', 
       'checkxDrpn' : [ 
        { key: 'Y',selectVal: "Yes"}, 
        { key: 'N',selectVal: "No"} 
       ] 
      }, 
      { 
       'fname':'raj', 
       'lname':'komali', 
       'email':'[email protected]', 
       'check' : 'N', 
       'checkxDrpn' : [ 
        { key: 'Y',selectVal: "Yes"}, 
        { key: 'N',selectVal: "No"} 
       ] 
      }, 
      { 
       'fname':'Roy', 
       'lname':'Mathew', 
       'email':'[email protected]', 
       'check' : 'N', 
       'checkxDrpn' : [ 
        { key: 'Y',selectVal: "Yes"}, 
        { key: 'N',selectVal: "No"} 
       ] 
      }]; 

in html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script> 

<table class="table table-striped table-bordered">          
    <tbody> 
    <tr ng-repeat="personalDetail in personalDetails"> 
     <td> 
     <select ng-model="personalDetail.selectedDpn" ng-init="personalDetail.selectedDpn = personalDetail.checkxDrpn[1].key" ng-options="option.selectVal for option in personalDetail.checkxDrpn track by option.key" value="personalDetail.checkxDrpn[1].key"></select>   
      default value : {{personalDetail.checkxDrpn[1].key}} 
     </td> 
    </tr> 
    </tbody> 
</table> 

Antwort

1

Sie benötigen die Modellvariable als Objekt zu setzen,

ng-init="personalDetail.selectedDpn= personalDetail.checkxDrpn[1]" 

DEMO

var app = angular.module('demo', []); 
 
app.controller("profileController", function($scope) { 
 
$scope.personalDetails = [ 
 
      { 
 
       'fname':'Muhammed', 
 
       'lname':'Shanid', 
 
       'email':'[email protected]', 
 
       'check' : 'Y', 
 
       'checkxDrpn' : [ 
 
        { key: 'Y',selectVal: "Yes"}, 
 
        { key: 'N',selectVal: "No"} 
 
       ] 
 
      }, 
 
      { 
 
       'fname':'John', 
 
       'lname':'Abraham', 
 
       'email':'[email protected]', 
 
       'check' : 'N', 
 
       'checkxDrpn' : [ 
 
        { key: 'Y',selectVal: "Yes"}, 
 
        { key: 'N',selectVal: "No"} 
 
       ] 
 
      }, 
 
      { 
 
       'fname':'raj', 
 
       'lname':'komali', 
 
       'email':'[email protected]', 
 
       'check' : 'N', 
 
       'checkxDrpn' : [ 
 
        { key: 'Y',selectVal: "Yes"}, 
 
        { key: 'N',selectVal: "No"} 
 
       ] 
 
      }, 
 
      { 
 
       'fname':'Roy', 
 
       'lname':'Mathew', 
 
       'email':'[email protected]', 
 
       'check' : 'N', 
 
       'checkxDrpn' : [ 
 
        { key: 'Y',selectVal: "Yes"}, 
 
        { key: 'N',selectVal: "No"} 
 
       ] 
 
      }]; 
 
    
 
});
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <link rel="stylesheet" href="style.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> 
 
<script src="script.js"></script> 
 
</head> 
 
<body ng-app="demo" ng-controller="profileController"> 
 
<table class="table table-striped table-bordered"> 
 
            
 
     <tbody> 
 
     <tr ng-repeat="personalDetail in personalDetails"> 
 
      <td> 
 
      <select ng-init="personalDetail.selectedDpn= personalDetail.checkxDrpn[1]" ng-options="value as value.selectVal for value in personalDetail.checkxDrpn track by value.key" 
 
     ng-model="personalDetail.selectedDpn" ></select> 
 
     
 
default value : {{personalDetail.checkxDrpn[1].selectVal}} 
 
      </td> 
 
     </tr> 
 
     </tbody> 
 
     </table> 
 
    
 
</body> 
 
</html>

+0

Danke Sajeetharan es funktioniert gut. Danke vielmals.. – raj

Verwandte Themen