Angular - amitbhilagude/userfullinks GitHub Wiki
- Overview
-
Component
- Split the entire page with different Units that can render and operate independently. E.g. Web Page will have Header, Footer, Left Pane, and Body and all can have different components. In a Typical Vanilla script model like Javascript and Jquery, you typically have Script and HTML tags separately however components will have Html and script.
- To render that component into Html it needs to have a tag defined which is called a Selector. Angular Module
- Angular Application will have nested components and it builds as a component Tree. It has a default main component called Root components and all components will be considered as child components. Similarly, each component can have nested components like the body that will have some units.
- Angular Evaluate {{ }} value within HTML. This expression can have members of components, arithmetic, or call method. More important is Function will get called if any member gets changed in a {{}}.
- *ngFor will be used for repeating the DOM element. *ngIf is used to render DOM if the condition becomes true. The difference between *ngIf and style display none is first will never create DOM element and second will have HTML but it is hidden. IF you need to have index in *ngFor then you can apend index as i.
- Input will be passed to the component similar like attribute e.g. . This will be set into component class using @input member name: type. This value will be set only after components are initialized so it will be empty if you try to use it in the constructor. You can set this in the ngonInit method. It is recommended to pass an object here instead of passing each property. You can set this in the constructor however it is best practice to keep the constructor lightweight and do such initialization in ngoninit method only.
- Angular has different page hooks life cycle. E.g. OnInit or OnInit changes. Details are in angular documents. These get called at different flows.
-
Setup Dev Environment
- Node JS: Running Javascript locally, Setup dev environment. It will also have npm installed to fetch npm package
- VS Code: IDE
- Angular CLI: Command npm install @angular/cli. All commands of cli start with ng. e.g ng -v
-
Folder Structure
- Creating a new project in Angular will have a different folder structure.
- Src: Where all code is written
- Components created under will have four files.
- HTML: To render html
- Typescript: Business logic
- CSS: Stylesheet
- spec.ts: Unit testing
- Each component has @componet and Class
- Class
- Properties and Methods components
- Decorator
- @components
- Selector: Name used for registering component in html
- Viewhtml
- StyleUrls : An array can have multiple style files paths or an inline css array. Limited to components
- @components
- Class
- Components created under will have four files.
- ng_modules: Where all dependencies are installed
- e2e: Used for an end to end testing
- Option is to use the protracter which is built using Selenium web driver. More details are here. https://medium.com/swlh/angular-end-to-end-testing-with-protractor-55897de591be
- Package.json: It has all dependencies. Angular dependencies are started with @angular and angular version will be available there.Run a project: command for running a project is ng serve. This will create an http server and host your angular application into that. It will create a URL for you to test that application.
- Index.html: Start up file
-
Handling Events
- In Javascript Event handling is done using attributes e.g. Button has OnClick event which will have javascript function to execute. In Angular, It uses a special syntax call (Click). The method will be defined within a component class. e.g <button (click)="MethodName"> . () Syntax indicates that data flow from View to Component and [] indicates that data flow component to view.
-
Two-way Data binding
- Two-way data binding is very rarely used in Angule so this functionality is not part of the Angular core module. It will require the import of Form modules in directives. It will expose ng-model which will be used in format [(ng-model)] indicates data flow in both directions. [()] Syntax is called Banana in the Box.
-
Modules
- Modules are similar to namespaces in c#. Each module will have multiple components. Modules are exported which will be used by any other modules. The module will be created as syntax modulename.module.ts. The module can be generated using ng cli command similar to the component command. It is best practice to define multiple modules in complex scenarios. Import will do on top is typescript import to find that component. The import we do within @ngmodule is Angular import.
- modulename.modules.ts has a module declaration. It has three main declarations @ngmodule annotations.
- Import: Import existing modules if modulename.modules.ts going to use this component
- Declarations: List of components created by modulename.modules.ts
- Export modulename.modules.ts so that it can be used by any other module with Import.
- Providers: List of Services. Service never part of any module but it needs to be defined to the closest module.
- Bootstrap
-
Annotations:
- Annotations are defined by Angular to identify the type of declaration. Most common Annotations are
- @component: For defining component
- @ng-module: For defining module
- @injectable: For defining services
- Annotations are defined by Angular to identify the type of declaration. Most common Annotations are
-
Service:
- Service can be defined if there no view involved. E.g. Calling backend API requires implementation which will be created in Service. If there is a View required then creating a component is a choice.
- Service is defined as part of Module using Providers. Service will have methods that can be called by components. To Import service in a component, it uses the dependency injection concept.
- Dependency injection is similar to.net core where let Angular creates an object for service provides it in the constructor. It is passed as a constructor argument and Angular handover that object. e.g. Constructor(svc: serviceName). where svc is an object created for service.
- Providers in @ng-module is used for defining services however it doesn't matter which component is going to use it as Service always defined in a shared container and can be used by any modules however best practice here to choose the closest modules and use providers to define service
-
HTTP Client
- Angular has an HTTPClient module to call async Rest API. It uses Observable and subscribes to receive an async response. This is similar to deferred and promise in Angular JS.
- You can inject this module into Service and call HTTPClient methods e.g. Get. It will return an object which can be called observable and it exposes the subscribe method which will be executed when we receive an async response. Subscribe method will have an input argument to capture the response and function to be executed. e.g. getDashboards(): Observable< Dashboards > { } method in the service and this.dashboardService.getDashboards().subscribe(loadedDashboards => { } is called.
-
- Build and Packaging
- ng-serve command creates a development server of nodejs and provides HTTP URL to test the application. This gives benefits to the developer to see the change runtime and all files published in the development server are in non minified mode.
- ng-build command creates a package for your application and makes it available to deploy web server, CDN. It excludes all dependencies of node and script is available to run in the browser. this package will still have an un-minified version
- ng-build --prod command creates the package with the minified version.
- Routing
- Angular provides a routing module. You can let angular know with --route during project creation to import this module.
- Routing modules that have a route object array. Route objects have multiple properties e.g. Path, Components. This will allow you to mention the routing path and which component needs to be rendered
- <\Router-Outlet> is a component part of the Routing module. Angular renders component inside this.
- If there is no path defined in url, redirect to the default route is done using RedirectTo Property in Route object where the path will be set an empty
- If any wrong path added, then you can create a new page not found component and map it to path **. Two * here means multiple words. could be separated by a slash.
- Child or Nested routing is done when you have one component and it will have multiple child components. Child path-based routing can redirect to a specific child component of the main component. This will be done using the Childrens property of routing method which again a route object array.
- Routing based on Menus will be done using Angular routing directly under the anchor tag. if we use Href then it will be full page refresh for SPA.
- Pipe
- Evaluate the value for a specific property.
- It is create ng generate pipe which will create a class which has input as transform. E.g. Truncate value for that variable then it will be used that class name after pipe |
- Angular style encapsulation is to make sure the style applied to the specific component. This can be changed by adding encapsulation property in @component. Angular has: Host used to apply styling to the parent component from a child but this is possible only after setting encapsulation property none.
- Nested component method call
- Calling method of the nested component is done by created a member variable of the nested component and call that class.