typescript - 如何使用 API 中的数据在离子选择中设置条件默认值
问题描述
我是 angular 7 和 Ionic 4 的新手。
我正在开发一个移动应用程序,我正在使用一个 API,我有一个任务列表,当我点击一个任务时,一个模式打开,显示任务信息,在这个模式中有一些字段是元素,例如分配的用户,我想根据创建任务时选择的分配用户来设置离子选择默认值。
总而言之,我希望如果分配了一个用户,当模态出现时,它将在离子选择中设置为默认值。
HTML 文件:
<ion-row>
<ion-col>
<ion-label>Asigned User:</ion-label>
<ion-select placeholder="Asigned User" formControlName="assignedUserId" >
<ion-select-option *ngFor="let user of simpleUsers|async;" [value]="user.id" (ionChange)="setUser(user.id)">
{{user.lastName}} {{user.firstName}}
</ion-select-option>
</ion-select>
</ion-col>
</ion-row>
TS 文件:
this.taskForm = this.formBuilder.group({
'title':[null],
'description' : [null],
'descriptionHtml':[null],
'assignedUserId': [null],
'deadline':[null],
'deadlineType':[null],
'fromDate':[null],
'clientId':[null]
});
// ...
async getTaskInfo() {
this.taskId = this.navParams.get('taskId');
await this.api.getTaskById(this.taskId).subscribe(result => {
this.task = result;
});
}
async getCompanies() {
this.clients = this.api.getCompanies();
}
async getSimpleUsers() {
this.simpleUsers = this.api.getSimpleUsers();
}
async setUser(userId) {
this.assignedUserId = userId;
}
async setClient(clientId) {
this.clientId = clientId;
}
解决方案
由于您将 绑定ion-select
到assignedUserId
控件并且将[value]
属性从ion-select-option
点绑定到 ,因此您user.Id
似乎唯一需要做的ion-select
就是assignedUserId
初始化. 它会是这样的:
// ...
async getTaskInfo() {
this.taskId = this.navParams.get('taskId');
// If you want to use await, getTaskById() should return a promise
this.task = await this.api.getTaskById(this.taskId).toPromise();
// I'm assuming the task has an userId property
if(this.task && this.task.userId) {
// Update the value of the control
this.taskForm.get('assignedUserId').setValue(this.task.userId);
}
}
// ...
推荐阅读
- python - 使用 android studio 应用程序获取 python 生成的数据
- tensorflow - TensorFlow 导入问题 Python 3.6
- python - Python Tkinter 条目未放置
- git - 当我运行“git gc”时如何自动跳过“文件取消链接失败”
- java - Spring Boot中的Apache Camel - 根据请求队列轮询消费者
- c# - 我如何在monogame中保存窗口大小?
- python - 使用 psycopg2 执行 psql 命令时出错?
- kubernetes - 在部署 Pod 的自定义资源定义中添加容器
- codeigniter - 分页在 codeignitor 框架中不起作用
- c# - FFImageLoading CircleTransformation 不适用于透明图像