简体   繁体   English

Angular2 ngFor-设置为选定

[英]Angular2 ngFor - set selected

When a user logs in to my app, it loads the previous UI setup that they were using. 当用户登录到我的应用程序时,它将加载他们正在使用的以前的UI设置。 To switch between interfaces there is a dropdown that lists all the options that specific user is authorized to use. 要在界面之间切换,有一个下拉列表列出了特定用户被授权使用的所有选项。

I would like the selected option in that list to reflect the previous UI when the page is loading but I'm having trouble coming up with how to set the selected <option> 我希望该列表中的选定选项能够在页面加载时反映以前的UI,但是我在想如何设置选定的<option>遇到问题

Here is some example code: 这是一些示例代码:

//User object from DB
var user = {
  username: admin,
  previousUI: 'Build',
  authorizedUI: [{title:'Default'}, {title:'Edit'}, {title:'Build'}]
}

html: HTML:

<form class="navbar-form">
  <label>System: </label>
  <select (change)="systemSelect($event.target.value)">
    <option *ngFor="let system of user.authorizedUI" [value]="system.title">
      {{system.title}}
    </option>
  </select>
</form>

In Angular2 how do I set the selected parameter on the option to reflect user.previousUI ? 在Angular2中,如何在选项上设置selected参数以反映user.previousUI

I tried adding this: 我尝试添加以下内容:

<option *ngFor="let system of user.authorizedUI" [value]="system.title" [selected]="system.title === user.previousUI">

But that didn't seem to do it. 但这似乎没有做到。

一种方法是

<select [ngModel]="user?.previousUI" (change)="systemSelect($event.target.value)">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM