title: How do I make a service function that takes a value and returns an observable in Angular 2?
body: <p>My Service:-</p>
  
  <pre><code>    @Injectable()
      export class MyService {
  
          doStuff(value){
               //Do stuff and when done return new data
  
               return newData;
          }
  
      }
  </code></pre>
  
  <p>My Component:-</p>
  
  <pre><code>    export class MyComponent implements OnInit {
          constructor(private _myService: MyService){}
  
          ngOnInit() {
              this._myService.doStuff(value)
              .subscribe((data) =&gt; {
                  console.log("new data: ", data);
              })
          }
      }
  </code></pre>
  
  <p>So my question is, how do I make the <code>doStuff()</code> function return an observable. Also I want to subscribe to said function whilst passing a value to it. How do I do this please?</p>
  
  <p>I have it working where by I set a variable as a new <code>Subject()</code> (inside my service) - then I call <code>.next()</code> on said variable and pass the value back. Only issue is I have to call my <code>doStuff()</code> function and then subscribe to the variable something like the below :-</p>
  
  <p>My current Service:-</p>
  
  <pre><code>    @Injectable()
      export class MyService {
          myData:any = new Subject&lt;any&gt;();
          myDataAnnounce = this.myData.asObservable();
  
          doStuff(value){
               //Do stuff and when done return new data
               this.myData.next(newData);
          }
  
      }
  </code></pre>
  
  <p>My current Component:-</p>
  
  <pre><code>    export class MyComponent implement OnInit {
          constructor(private _myService: MyService){}
  
          ngOnInit() {
              this._myService.doStuff(value);
              this._myService.myDataAnnounce
              .subscribe((data) =&gt; {
                  console.log("new data: ", data);
              })
          }
  
  
      }
  </code></pre>
  
  <p>I would like to make one call with the value I want to pass and then subscribe. Thanks in advance for any help you can provide.</p>