Git Product home page Git Product logo

angular-lifecycle-hooks's Introduction

lifecycle-hooks

Built With

Getting Started

Prerequisites

Project Setup (VS Code)

  • Angular

    • Install Angular CLI globally (one time setup)
    npm install -g @angular/cli
    ng version
    • Open project in vsCode (command run in the project directory)
    code .
    • Create Angular App
    ng new project-name
    ng serve
  • Create new Angular Component

    ng generate component component-name
    • Create new Angular Service
    ng generate service service-name

Topics practiced to get things done

Angular

-Lifecycle hooks

Error messages for future reference

✖ Error: Invalid Character (typing ng --version in the terminal)
Solution: Use bash (workaround)
✖ Error: Property 'name' has no initializer and is not definitely assigned in the constructor.
Solution: Add "strictPropertyInitialization": false in the compiler options of the tsconfig.json or a default value to the property.
✖ Error: Can't bind to 'ngModel' since it isn't a known property of 'input'.
Solution: Add FormsModule and ReactiveFormsModule to the imports array in app.module.ts.

Comments

Constructor

Although not a lifecycle hook, constructor is the first thing called when the component is instantiated. At this moment, the component properties are not accessed yet.

ngOnChanges

Runs everytime component properties are updated. The first update occurs before OnInit. It doesn't run when event is fired up with no property change. It has a SimplesChanges parameter that allows us to access the previous current values of a specific property and check if it's the first change.

ngOnInit

We need to import and implement OnInit in the class declaration. It is fired once, just after the first change detection cycle. By this time the properties are already accessible and updated. That's the best place to add inilialization logic for the component.

ngDoCheck

It gets called any time the change detection cycle runs. Not just for component propertiy changes, but for anything else. If run an event without changing properties, onchange won't run, but ngDoCheck will.

ngAfterContentInit

This lifcycle hook gets called when all component content has been fully initialized. The content is all html and properties inside the component selector. It is run only once.

ngAfterContentChecked

Gets called whenever the content in the selector changes.

ngAfterViewInit

Gets called when the components view and all its child views are fully initialized. It gets called only once during the first change detection cycle.

ngAfterViewChecked

Gets called whenever there's a change event detection cycle.

ngOnDestroy

ngOnDestroy is run when the component is removed from the DOM. Great place to do some cleanup work.

angular-lifecycle-hooks's People

Contributors

marcellpaganini avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.