angularjs - how to assign default values to dropdown from backend response -


i have local $scope.countries in html populate country state city , instead of select default values in dropdown , need show country state city values coming backend default.for example , afganistan , badhakshan .

plunker code here. http://plnkr.co/edit/dpoofrkgxo28tdxzge5b?p=preview

<html lang="en-us">     <head>         <meta charset="utf-8">          <link href="css/custom.css" rel="stylesheet">         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">     </head>     <body ng-controller="mainctrl">         <form action="#" role="form" class="form-horizontal" id="location" method="post" accept-charset="utf-8">         <div class="form-group">      <div class="col-sm-4">         <select name="country" ng-model="model.country" class="form-control countries" id="countryid" required="required"> <option value="">select country</option> </select>     </div> </div>  <div class="form-group">   <div class="col-sm-4">         <select name="state" ng-model="model.state" class="form-control states" id="stateid" required="required"> <option value="">select state</option> </select>     </div> </div>  <div class="form-group">   <div class="col-sm-4">         <select name="city" ng-model="model.city" class="form-control cities" id="cityid" required="required"> <option value="">select city</option> </select>     </div> </div> </form>      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>      <script src="app.js"></script>        <script src="location.js"></script>  </body> </html> 

js:

    var app = angular.module('plunker', []);  app.controller('mainctrl', function($scope) {     var response = {                 "response":                {"json":                {"session_id":"498",                "profile":{"country":"afghanistan",                "state":"badakhshan","city":"eshkashem",                "pincode":"54564",                "rolemandatory":1},                "roles":[]}}}   $scope.getprofile = function () {                 $scope.model.country = response.response.json.profile.country;                 $scope.model.state = response.response.json.profile.state;                 $scope.model.city = response.response.json.profile.city;             };   }); 

sample code multilevel dropdown. there many ways achieve this..

function myctrl($scope) {      $scope.selecteddist = {};      $scope.selectedthana = {};      $scope.districts = [          {id: 1, name: 'delhi'},          {id: 2, name: 'mumbai'},          {id: 3, name: 'chennai'}      ];        $scope.thanas = [          {id: 1, name: 'mirpur', did: 1},          {id: 2, name: 'uttra', did: 1},          {id: 3, name: 'shahabag', did: 1},          {id: 4, name: 'kotalipara', did: 2},          {id: 5, name: 'kashiani', did: 2},          {id: 6, name: 'moksedpur', did: 2},          {id: 7, name: 'vanga', did: 3},          {id: 8, name: 'faridpur', did: 3}      ];        $scope.localelist = [          {id: 1, name: 'ulhasnagar', tid: 1},          {id: 2, name: 'ambarnath', tid: 1},          {id: 3, name: 'kalyan', tid: 5},          {id: 4, name: 'vithalvadi', tid: 2},          {id: 5, name: 'vikhroli', tid: 6},          {id: 6, name: 'kanjurmarg', tid: 2},          {id: 7, name: 'ghatkopar', tid: 3},          {id: 8, name: 'kamlakar nagar', tid: 3},          {id: 9, name: 'kolsewadi', tid: 4},          {id: 10, name: 'fort', tid: 7},          {id: 11, name: 'gudgava', tid: 8},          {id: 12, name: 'mayur vihar', tid: 8},          {id: 13, name: 'chinchpokli', tid: 6}      ];        $scope.filterthana = function (thana) {          return (thana.did === $scope.selecteddist.id);      };      $scope.filterlocale = function (locale) {          return (locale.tid === $scope.selectedthana.id);      };  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />      <div ng-app ng-controller="myctrl">           <div class="col-xs-6">              <form class="form-horizontal" name="testform">                 <div class="form-group">                    <div class="col-md-3"><label><i class="fa fa-question-circle fa-fw"></i> district list</label></div>                    <div class="col-md-4">                       <select class="form-control" name="dist" required ng-model="selecteddist" ng-options="district.name district in districts">                          <option value="">select</option>                       </select>                    </div>                 </div>                 <div class="form-group">                    <div class="col-md-3"><label><i class="fa fa-question-circle fa-fw"></i> thana list</label></div>                    <div class="col-md-4">                       <select class="form-control" name="thana" required ng-model="selectedthana" ng-options="thana.name thana in thanas | filter: filterthana">                          <option value="">select</option>                       </select>                    </div>                 </div>                 <div class="form-group">                    <div class="col-md-3"><label><i class="fa fa-question-circle fa-fw"></i> locale list</label></div>                    <div class="col-md-4">                       <select class="form-control" name="local" required ng-model="selectedlocale" ng-options="locale.name locale in localelist | filter: filterlocale">                          <option value="">select</option>                       </select>                    </div>                 </div>              </form>           </div>          </div>


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -