Fireship · @fireship

Learn NgModule in Angular with Examples

Uploaded July 7, 2017 · Length 5:59

Learn NgModule in Angular with Examples

Summary

ngModule in Angular: organizing application structure with examples

  • angular ngModule
  • angular application structure
  • module organization
  • angular components
  • angular services

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    in this episode we're going to talk

  2. 0:01

    about ng module and how to properly use

  3. 0:04

    it to organize an angular application so

  4. 0:07

    what's the purpose of an ng module in

  5. 0:09

    the first place the answer is it helps

  6. 0:12

    organize the complex relationship

  7. 0:14

    between views such as components and

  8. 0:16

    directives to data providers such as

  9. 0:18

    services and guards it works by allowing

  10. 0:21

    you to define all these relationships in

  11. 0:23

    the Swingle file as opposed to doing it

  12. 0:25

    individually and every component service

  13. 0:28

    as was the case in early days of angular

  14. 0:30

    2 now let's look inside an NG module and

  15. 0:34

    see what these metadata terms actually

  16. 0:36

    mean first we have declarations which is

  17. 0:38

    where you would put any component

  18. 0:40

    directive or pipe and then it'll be

  19. 0:42

    privately available to that module if

  20. 0:44

    you want it to be publicly available to

  21. 0:46

    other modules then you'll need to add it

  22. 0:49

    to the export section this concept is

  23. 0:52

    similar to public and private functions

  24. 0:54

    and object-oriented programming

  25. 0:56

    components will be private by default

  26. 0:58

    and become public if you add them to

  27. 1:00

    exports other modules gain access to

  28. 1:03

    exported components by importing that

  29. 1:05

    module in their own metadata

  30. 1:09

    so just to recap declarations imports

  31. 1:12

    and exports define the relationship

  32. 1:14

    between components and how they're

  33. 1:16

    shared between different ng modules next

  34. 1:20

    we have the bootstrap property which

  35. 1:22

    defines a component that's used to

  36. 1:24

    initially load your application by

  37. 1:26

    default this will usually be the app

  38. 1:28

    component which is created automatically

  39. 1:30

    by the angular CLI the final property

  40. 1:34

    you should be aware of is providers this

  41. 1:37

    is where you register data providers

  42. 1:38

    such as Services and guards that can be

  43. 1:40

    injected into components within this

  44. 1:42

    module the injectable class will be

  45. 1:45

    available to any component in this

  46. 1:47

    module as well as any module that

  47. 1:49

    imports it

  48. 1:50

    now let's look at some practical ways we

  49. 1:53

    can use energy module to organize an

  50. 1:55

    application in this example we have all

  51. 1:58

    of our components and services in the