我正在尝试使用 ng-selected 设置默认选定值,但它不起作用。
我的代码是
<select
id="department"
name="department"
class="form-control"
ng-model="editProfilCtrl.infos.department"
ng-change="editProfilCtrl.findCities()">
<option
ng-repeat="dep in editProfilCtrl.infos.departments"
class="text-uppercase"
value="{{dep}}"
ng-selected="{{dep.idDepartment == editProfilCtrl.infos.department}}">
{{editProfilCtrl.infos.department}} - {{dep.idDepartment}} - {{dep.code}} - {{dep.name}}
</option>
</select>
在我的下拉列表中,我有以下结果:
- (空的)
- 1 - 1 - Code01 - TEST1
- 1 - 2 - Code02 - TEST2
- 1 - 3 - Code03 - TEST3
- ...
你可以看到editProfilCtrl.infos.department = 1。所以,通常它必须默认选中。但这是被选中的空行:/。你有想法吗 ?
谢谢。
编辑:mac gyver “解决方案”:
<select
id="department"
name="department"
class="form-control"
ng-model='editProfilCtrl.infos.department'
ng-change="editProfilCtrl.findCities()"
ng-options='dep as dep.code + " - " + dep.name for dep in editProfilCtrl.infos.departments'>
</select>
在我的控制器中:
self.infos.selectedDepartment = editProfilFormDto.department;
self.infos.departments = editProfilFormDto.departments;
self.infos.department = self.infos.departments[self.infos.selectedDepartment - 1];