2015-03-19 74 views
1

当您选择一个名称时,我得到了一个自动填充的表单。HTML AngularJS - 选择绑定

<select ng-model="selected" ng-options="user as user.name for user in users" > 
</select> 

所以,当我选择我的用户,下一个字段(地址,电子邮件,电话,...)将填充所选用户的数据。

<input type="text" id="address" value="{{selected.address}}"> 

我的问题是,我有一个选择所有的国家,我想获得选定的国家显示。

<select class="form-control"> 
      <option value="AF">Afghanistan</option> 
      <option value="AX">Aland Islands</option> 
      <option value="AL">Albania</option>..... 

所以,当我选择“马克”谁是住在美国,美国,我想选择的国家,以显示“美的”,而不是第一个选项“阿富汗”的。

希望我的问题已经够清楚了。

Thx!

+0

使用NG选项和NG-模型选择太像'NG- model =“user.country_code”ng-options =“c.code as c.name for c in countries”' – YOU 2015-03-19 11:07:29

回答

0

您可以使用ng-selected指令。

假设你选择的人都有它具有国家代码country属性,并且您有国家的数组对象的国家{code: 'AF', name: 'Afganistan'}

<select ng-model="selected.country" > 
    <option ng-repeat="country in countries" 
      value="{{country.code}}" 
      ng-selected="country == selected.country"> 
     {{country.name}} 
    </option> 
</select> 
+0

Without ng-model =“selected.country”它完美的工作thx – Weedoze 2015-03-19 13:02:41

0

DEMO

<select ng-model="selected" ng-options="user as user.name for user in users" > 
</select> 

    <select class="form-control" ng-model="selected.country"> 
      <option value="AF">Afghanistan</option> 
      <option value="AX">Aland Islands</option> 
      <option value="AL">Albania</option> 

    </select> 

在你的控制器:

angular.module("SO29142872", []) 
    .controller("MainCtrl", function($scope) { 

    $scope.users = [{ 
     "name" : "Mohamed RIas", 
     "country" : "AX" 
    },{ 
     "name" : "Weedoze", 
     "country" : "AL" 
    }] 

});