angular - 当使用服务将数据从一个组件传输到另一个组件时,如何正确更新数据?
问题描述
我正在将数据从 X 组件传输到 Angular 中的 Y 组件(顺便说一下,我是 Angular 的新手)显示数据运行良好并且各种情况,现在问题出在我编辑/更新该信息时
我已经尝试过调整这些功能,但似乎我的数据绑定是错误的。
<pre>{{tasklist | json}}</pre> >> //file that is coming from the service
// this is showing properly
<form (ngSubmit) = "updateTask(tasklist)">
<mat-form-field>
<mat-select placeholder="Choose Project" name="projectlist">
<mat-option value="{{prjlist.projectname}}" *ngFor="let prjlist of prjlist">{{prjlist.projectname}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select placeholder="Target" name="objectivelist">
<mat-option value="{{objlist.objectivedescription}}" *ngFor="let objlist of objlist">{{objlist.objectivedescription}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select placeholder="Goal" name="krslist">
<mat-option value="{{krlist.keyresultname}}" *ngFor ="let krlist of krlist">{{krlist.keyresultname}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="inputlong">
<input matInput placeholder="Task" name="task" value="{{tasklist.taskname}}">
</mat-form-field>
<mat-form-field>
<mat-select placeholder="Assign To" name="Userlist" >
<mat-option value="{{userslist.firstname}} {{userslist.lastname}} ({{userslist.email}})" *ngFor = "let userslist of userslist">{{userslist.firstname}} {{userslist.lastname}} ({{userslist.email}})</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<input matInput [matDatepicker]="picker" placeholder="Choose a deadline" name="datepicker">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<button class="btn" mat-button>Update</button>
</form>
export class TaskEditComponent implements OnInit {
//hold services
tasklist='';
constructor(//import all services here so that it can show the data in the component of add task
private taskServices: TaskService,
private objsServices : ObjectivesService,
private prjServices:ProjectsService,
private userServices:UsersService,
private krsServices:KrsService,
private editServices:EdittaskService
) { }
//populate the dropdown menus
prjlist : prjmodels[];
objlist : objsmodel[];
krlist:krsmodel[];
userslist:usersmdl[];
listtask : taskdb[];
editedtask : taskdb ={
taskname: '',
status:1,
aging:false,
assignfrom: '',
assignto: '',
deadline: '',
project: '',
objective: '',
keyresults: '',
}
ngOnInit() {
this.objsServices.getobjs().subscribe(objsobs =>{
console.log(objsobs);
this.objlist=objsobs;
});
this.prjServices.getprojects().subscribe(prjobs =>{
console.log(prjobs);
this.prjlist = prjobs;
});
this.userServices.getUsers().subscribe(userobs =>{
console.log(userobs);
this.userslist=userobs;
});
this.krsServices.getkrs().subscribe(krsobs =>{
console.log(krsobs);
this.krlist=krsobs;
});
this.taskServices.gettask().subscribe(listtasksobs =>{
console.log(listtasksobs);
this.listtask = listtasksobs;
})
this.editServices.sharetask.subscribe(x => this.tasklist = x)
}
updateTask(tasklist:taskdb){
this.taskServices.updateTask(tasklist);
}
}
updateTask(tasks:taskdb){
this.taskDoc = this.tasklist.doc(`tasks/${tasks.id}`);
this.taskDoc.update(tasks);
}
解决方案
尝试在您的服务中创建一个Subject
对象,如下所示。
tasks = new Subject<any>();
每当您更新任务对象时,请调用对象上的方法next
来Subject
发布您的新数据,如下所示。
updateTask(newTasksObject){//blah //blah
this.tasks.next(newTasksObject);}
现在,您希望保持数据同步的每个组件,例如您的任务列表对象,只需订阅Subject
您已经在服务中创建的对象,如下所示。
yourService.tasks.subscribe(res=>{
//do your thing here, the res object is your newTasksObject mentioned earlier in the update method})
问候,
推荐阅读
- c# - 登录 Blazor 应用程序时重定向错误
- javascript - 表单仅在 safari 上不使用 javascript 提交
- c# - 从数组中创建所有变体而不重复 [C#]
- java - 由于某种原因在 Vaadin 7 中未创建 Servlet
- java - Spring 如何“神奇地”知道要使用哪种 Bean 类型?
- c - 在 Ada 中访问 C 字符串时出现分段错误
- reactjs - MainApplication.java 上的 React-Native-Reanimated 错误
- java - 降序排序
- java - 如何模拟特定的阅读文件?
- python - Python函数,if语句不起作用