search

Home  >  Q&A  >  body text

javascript - How is dependency injection implemented in angular2

http://www.alloyteam.com/2015...

This article briefly talks about the principle of dependency injection in angularjs. I want to know how ng2 implements dependency injection?

过去多啦不再A梦过去多啦不再A梦2755 days ago1022

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-07-04 13:46:32

    The injection of

    angular2 can actually be completed in the constructorconstructor.
    For example, the example you posted contains injection http, which can be written as

    in angular2
    constructor(private http: Http){}

    The prerequisite is to reference the Http module

    import { Http } from '@angular/http';

    Where you need to call the http method, you only need

    this.http.get()....

    That’s it.
    Other methods are also similar, you can check the official website for details

    reply
    0
  • 巴扎黑

    巴扎黑2017-07-04 13:46:32

    I recommend you an introductory video to ng2, watch the second section

    reply
    0
  • Cancelreply