在尝试将[(ngModel)]
放在选择标记上时,所选选项不再出现在加载页面上。在将其放置在select标签本身上之前,默认选项State
刚好在我第一次加载页面时显示出来。这是我目前的代码。
HTML:
<select class="form-control" id="inputState" name="inputState" [(ngModel)]="newJob.state" required>
<option value="" invalid selected>State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
TS:
export class NewJobListingComponent implements OnInit {
isDeleted: boolean = false;
currentTag: string = '';
maxTags: boolean = false;
newJob: JobListing = {
jobTitle: '',
city: '',
state: '',
department: '',
type: '',
tags: []
};
}
正如我所说,在我将[(ngModel)]
放在<select ...>
上之前,下拉框在页面首次呈现时正确显示了State
,现在却没有。我什至尝试将newJob.state初始化为'State',但这也不起作用。我在做什么错了?
答案 0 :(得分:1)
通过将默认选项样式设置为display: none
,尝试以下方法,它对我有用。当用户默认情况下会看到下拉列表,他们将看到State
时,它将充当占位符;打开时,将不会显示State
。那是hack。它就像一个占位符。
<option value="" style="display: none">State</option>
答案 1 :(得分:0)
我改变了
newJob: JobListing = {
jobTitle: '',
city: '',
state: '',
department: '',
type: '',
tags: []
};
转到下面的代码,即可正常工作
newJob: any = {
jobTitle: "",
city: "",
state: "",
department: "",
type: "",
tags: []
};