【发布时间】:2018-08-29 07:04:31
【问题描述】:
你能帮我解决我的问题吗?我真的不明白为什么它不起作用。
我想从数据库的 Level[] 动态生成选择
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
dictionary-item.component.html:
<div class="pull-right level">
<select id="levelSelect" ng-model="levelModel" ng-options="level as level.level for level in levels" (click)="stopPropagation($event)">
</select>
</div>
dictionary-item.component.ts:
@Component({
selector: 'app-dictionary-item',
templateUrl: './dictionary-item.component.html',
styleUrls: ['./dictionary-item.component.css']
})
export class DictionaryItemComponent implements OnInit {
@Input() dictionaryItem: DictionaryItem;
levels: Level[] = [];
constructor(private dictionaryService: DictionaryService) { }
ngOnInit() {
this.levels = this.dictionaryService.getLevels();
console.log(this.levels);
/*
from Chrome console:
(5) [{…}, {…}, {…}, {…}, {…}]
0
:
{id: 1, level: 1}
1
:
{id: 2, level: 2}
2
:
{id: 3, level: 3}
3
:
{id: 4, level: 4}
4
:
{id: 5, level: 5}
length
:
5
__proto__
:
Array(0)
*/
}
}
this.dictionaryService.getLevels() 返回 Level[]:
其中级别:
export class Level {
public id: number;
public level: number;
constructor(id: number, level: number) { }
}
为什么 Angular 在页面上生成一个空白的选择列表?没有任何价值观?
我什至尝试在组件中设置:
ngOnInit() {
this.levels = [new Level(1,1), new Level(2,2), new Level(3,3)];
}
但网站上没有任何代。什么都没有。
提前感谢您! 马特乌斯
【问题讨论】:
标签: angular select drop-down-menu ng-options angularjs-ng-options