title: How to call service method from html file.?
body: <p>Lets explain my code, I am having one component file and one service file</p>
  
  <p>First, From the component I am passing the values to the service file's methods and storing the values as global variable in service file.</p>
  
  <p>Second, I am calling another one method in the component file, and I am trying to access the global variable, what I stored earlier. </p>
  
  <p>My code is below</p>
  
  <p>Component file</p>
  
  <pre><code>import { NewService } from './new.service';
  
  
  
  @Component({
  selector: 'test',
  templateUrl: './passwordPolicy.html',
  providers: [FormErrorService]
  })
  export class TestComponent implements OnInit {
  
      obj1 = {"val":1,"val2":2};
      obj2 = {"val":1,"val2":2};
  
      constructor(private ns: NewService,) {
  
      }
      ngOnInit(){
          this.ns.firstMethod(this.obj1,this.obj1);
      }
  
      keyCheck(){
          this.ns.secondMethod();
      }
  
  }
  </code></pre>
  
  <p>Service file</p>
  
  <pre><code>export class NewService{
      Sobj: any;
      Sobj2: any;
      firstMethod(v1,v2){
          this.Sobj = v1;
          this.Sobj2 = v2;        
      }
      secondMethod(){
          console.log(this.Sobj);
          console.log(this.Sobj2);
      }
  }
  </code></pre>
  
  <p>and the template file</p>
  
  <pre><code>&lt;button (click)='keycheck()'&gt;Enter&lt;/button&gt;
  </code></pre>
  
  <p>When clicking the button I am getting console.log with the values.</p>
  
  <hr>
  
  <p>Now my question is I am trying to the call the service method from the Html file, like as follow</p>
  
  <p>component file</p>
  
  <pre><code>    sMet:any;
      constructor(private ns: NewService,) {
          this.sMet = this.ns.secondMethod;
      }
  </code></pre>
  
  <p>And the html file is </p>
  
  <pre><code>&lt;button (click)='sMet()'&gt;Enter&lt;/button&gt;
  </code></pre>
  
  <p>but above code is giving service's object are undefined. How to call it.?</p>
  
  <p><a href="https://stackblitz.com/edit/angular-nxtfty?file=src%2Fapp%2Fapp.component.html" rel="noreferrer">Demo</a></p>