How to bind dropdown control in edit mode?

不打扰是莪最后的温柔 提交于 2019-12-01 08:59:05

Here is what you wanted you could use ng-options for both select box for states & countries.

HTML

<td>
    <p data-ng-hide="friend.editMode">{{ friend.Country.Name }}</p>
    <select data-ng-show="friend.editMode" ng-model="friend.Country" ng-options="country.Name for country in countries track by country.Id "></select>
</td>
<td>
    <p data-ng-hide="friend.editMode">{{friend.State.Name }}</p>
    <select data-ng-show="friend.editMode" ng-model="friend.State" ng-options="state.Name for state in states track by state.Id "></select>
</td>

Controller

$scope.countries = [{
    "Id": 1,
    "Name": "America",
}, {
    "Id": 2,
    "Name": "Australia",
}, {
    "Id": 3,
    "Name": "london",
}];

$scope.states = [{
    Id: 1,
    CountryId: 1,
    Name: "Chicago",
}, {
    Id: 2,
    CountryId: 2,
    Name: "sydney",
}, {
    Id: 3,
    CountryId: 3,
    Name: "abc",
}];

Working Fiddle

Note

Used track by id for selecting option on load as suggest in this github issue

You can use ng-repeat

<select data-ng-show="friend.editMode" ng-model="friend.Country.Name">
    <option ng-repeat="country in countries" value="{{country}}" ng-selected="friend.Country.Name==country">{{country}}</option>
</select>   

Or use ng-options

<select ng-model="friend.Country.Name" ng-options="country as country for country in countries"></select>

In your controller define an array of countries.

$scope.countries = ['America', 'Australia', 'london'];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!