Skip to content

Instantly share code, notes, and snippets.

@misostack
Last active June 9, 2022 03:24
Show Gist options
  • Select an option

  • Save misostack/34c91c01eb462ec0bcfa8ecd468de048 to your computer and use it in GitHub Desktop.

Select an option

Save misostack/34c91c01eb462ec0bcfa8ecd468de048 to your computer and use it in GitHub Desktop.
100 days Angular

100 days Angular

Component

Lifecycle

image

Encapsulation

ViewEncapsulation.Emulated

encapsulation: ViewEncapsulation.Emulated, // this is default view encapsulation

image image

ViewEncapsulation.None

encapsulation: ViewEncapsulation.None,

image

ViewEncapsulation.ShadowDom

encapsulation: ViewEncapsulation.ShadowDom,

image

It does not effect by stylesheets in this page

If there is no styles or styleUrls in ViewEncapsulation.Emulated mode, this component will automatically set to ViewEncapsulation.None

Component Interaction

Parent to Child

Input

image image

Template Reference

Parent interacts with child using local variable

Component Ref

image image image

ElementRef

image image image

Parent and children communicate using a service

image image image

Why unsubscribe unsubscription is important?

Let's take a look at this example

image image

Everytime, user move to another screen and go back to the screen contain this component. Number of observers increased! image

So how we fixed it? Easy peasy!

image

image image image

Component Styles

:host {
  font-style: italic;
}

:host(.active) {
  font-weight: bold;
}

:host-context(.active) { // same as :host()
  font-style: italic;
}

// The following example targets all <h3> elements, from the host element down through this component to all of its child elements in the DOM.

:host ::ng-deep h3 {
  font-style: italic;
}

/deep/ ~ ::ng-deep >>>

How many ways?

Inline

image

Style files in component metadata

image

Template Inline Style

image

Template link tags

image

CSS @imports

image

Global styles

image

Component Communication

1. Open Modal ( Modal is children Component )

image image

Event Listener

image

Key Press Listener

image

FullList Event

https://github.com/angular/angular/blob/master/packages/compiler/src/schema/dom_element_schema_registry.ts#L78

https://www.w3.org/TR/DOM-Level-3-Events-key/#named-key-attribute-values

Search "click" or "keypress"

Parent image image image

Content Projection

Content projection is a pattern in which you insert, or project, the content you want to use inside another component

Single-slot content projection

image image

Multi-slot content projection

image image

Conditional content projection

ng-template

image

ng-container

image

image

Template

Directives

image

Built-in attribute directives

NgClass: Adds & Removes Set of CSS Classes

NgStyle: Adds & Removes Set of HTML Styles

image image

NgModel: Adds two-way data binding to an HTML form element.

image

Built-in structural directives

NgIf

image

NgFor

Without trackby image

When the entire list has been prefetch, entire component will be rerendered image

With tracky

Only the item changed will be rerendered image image image

NgSwitch(attr), NgSwitchCase(structural), ng-container

image

Custom Attribute Directive

image image

Angular pass callback function to child component as input

methodName.bind(this, args);

image image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment