我正在将数据从Angular中的X组件传输到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);
}
答案 0 :(得分:0)
尝试在服务中创建一个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})
致谢