【发布时间】:2020-05-01 10:24:06
【问题描述】:
我对 Angular 还很陌生,但除非我从我的 Mat Select(用于我的 GraphQL 突变)中删除 ngModel,否则它不会呈现默认值。我的目标是让下拉菜单预先填充用户的当前位置。
<mat-list-item>
<mat-form-field>
<mat-label>Location</mat-label>
<mat-select [value]="user.position" matNativeControl required [(ngModel)]="newLocation">
<mat-option value="Top">Top</mat-option>
<mat-option value="Front">Front</mat-option>
<mat-option value="Middle">Middle</mat-option>
<mat-option value="Back">Back</mat-option>
<mat-option value="Right">Right</mat-option>
<mat-option value="Left">Left</mat-option>
</mat-select>
</mat-form-field>
</mat-list-item>
这是 TS 文件:
updateUser(userId) {
this.apollo
.mutate({
mutation: EDIT_USER,
variables: {
input: {
userPosition: this.newLocation,
}
}
})
.subscribe(
({ data }) => {
if (data['editUser'].complete) {
this.getAssets();
const user = this.assetDetails.users.find((s) => s.id === userId);
user.userPosition = this.newLocation;
}
},
(error) => {}
);
}
我已经验证 user.position 给了我正确的值,因为它在 [(ngModel)]="newLocation" 被删除后呈现,但就像我之前所说的,我的突变需要它。我似乎无法找到解决方法。感谢您的任何意见。
【问题讨论】:
标签: angular select drop-down-menu default-value