【问题标题】:select ng-model ng-options doesn't work选择 ng-model ng-options 不起作用
【发布时间】: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


    【解决方案1】:

    它不起作用,因为您没有使用 angularJS。

    <select [(ngModel)]="levelModel" (click)="stopPropagation($event)">
      <option *ngFor="let level of levels" [value]="level">{{ level }}</option>
    </select>
    

    【讨论】:

    • AAaaaaaaaaaa,没问题。这对我来说很奇怪......我很惊讶这是什么......但没有信息表明它不是 angularjs......谢谢你的帮助!
    • 没问题,如果您的问题已解决,请务必将其标记为已解决。
    猜你喜欢
    • 2015-05-06
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2017-04-29
    • 1970-01-01
    • 2016-05-02
    相关资源
    最近更新 更多