title: How to use Dependency Injection in Angular 6 export function
body: <p><strong>I have tried this</strong></p>
  
  <pre><code>export function uniqueUserNameValidation(commonsService: CommonsService): AsyncValidatorFn {
    return (c: AbstractControl): Promise &lt; ValidationErrors | null &gt; | Observable &lt; ValidationErrors | null &gt; =&gt; {
      return commonsService.uniqueUserName(c.value).pipe(
        map(data =&gt; {
          return data &amp;&amp; data.status ? {
            'uniqueValidation': true
          } : null
        })
      )
    }
  }
  </code></pre>
  
  <p><strong>Common Services Method</strong></p>
  
  <pre><code>uniqueUserName(name:string){
          return this.http.get&lt;booleanData&gt;(constDefault.API_URL+"/commons/nameAlreadyExist/"+name+"/Tank")
  }
  </code></pre>
  
  <p><strong>I am getting this error</strong></p>
  
  <blockquote>
    <p>core.js:1673 ERROR Error: Uncaught (in promise): TypeError: Cannot
    read property 'uniqueUserName' of undefined
        TypeError: Cannot read property 'uniqueUserName' of undefined</p>
  </blockquote>