[Angular 2] 6. Factory Provider

Source: Internet
Author: User

In this lesson, we discuss how and if to use factory providers, to enable dependencies that shouldn ' t is available to an Gular ' s DI.

If you have the This service:

class Loggerprovider {    Constructor (Enabled:boolean) {        if(enabled) {            Console.log ( " Logger is enabled " )        }    }}

which requires you provide Param in constructor function.

If you just the it like normal provider:

@Component ({   'todos',   providers: [Todoservice, Loggerprovider],   Template: ' ... '  })

It won't work, because when init the Loggerprovider instance, we need to pass a Boolean value as a param.

But you can use the factory provider for this:

providers: [    todoservice   , {        = =             {returnnew Loggerprovider (true)        }    }],    

[Angular 2] 6. Factory Provider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.