Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用服务的组件之间的角度2双向绑定_Angular_Typescript - Fatal编程技术网

Angular 使用服务的组件之间的角度2双向绑定

Angular 使用服务的组件之间的角度2双向绑定,angular,typescript,Angular,Typescript,我需要将一个对象从一个组件传递到另一个服务,并由另一个组件作为可观察对象进行获取,以便将来当我更改有问题的组件时,它会更新服务,但当前getQuestion()不适用于有问题的组件 服务: selectedSurvey: any; setQuestion(passedObject){ this.selectedSurvey= passedObject; } getQuestion(): Observable<any>{ return this.

我需要将一个对象从一个组件传递到另一个服务,并由另一个组件作为可观察对象进行获取,以便将来当我更改有问题的组件时,它会更新服务,但当前getQuestion()不适用于有问题的组件

服务:

   selectedSurvey: any;
   setQuestion(passedObject){ 
   this.selectedSurvey=  passedObject;
   }

   getQuestion(): Observable<any>{
  return this.selectedSurvey;
   }
调查组件Html

<div class="container">
    <div class="form-group">
        <label for="sel1">Select Survey:</label>
        <select class="form-control" [(ngModel)]="selectedObject" id="selsurvey" (ngModelChange)="onChange($event)">
          <option *ngFor="let s of survey" [value]="s">{{s.surveyName}}</option>

        </select>
      </div>
</div>
在职:

yourSubject:Subject<WhatevaTypeYouWant>=new Subject<WhatevaTypeYouWant>();
在消费者中

  service.yourSubject.subscribe(val=>{handler});

这里有一个工作示例:

检查
子组件/child component.ts
子组件/communication.service.ts
app.component.ts

希望能有帮助

yourSubject:Subject<WhatevaTypeYouWant>=new Subject<WhatevaTypeYouWant>();
service.yourSubject.next(value);
  service.yourSubject.subscribe(val=>{handler});