We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 32
nv oll >hackr.io Cheat w\9 Silay : Before we get started, let's touch briefly upon Angular JS. The background will help you understand the greater purpose behind using the Angular framework in your work, What is Angular JS? Google developed AngularJS, a Javascript framework that helps developers to create fully scaled single-page web apps. The term "single page" refers to the necessity for web pages to be updated A single web page consists of multiple moving parts and their organization— navigation bar, footer, sidebar, and more. To inject these components into the same web page, we use AngularJs, Bottom line? AngularJS makes the web page dynamic. Instead of refreshing the page when a user navigates to a URL, AngularJS injects the needed components into the same page. It basically reuses the components that don't change, which cuts down on load time and offers a better browsing experience. Now, let's get into some Angular commands.Angular CLI Cheat Sheet The Angular command-line interface (CLI) is a set of commands that help you initialize, develop, and maintain highly scalable and speedy Angular apps directly from the command shell. In this Angular CL! commands cheat sheet section, we'll cover various Angular Cl commands. 4. Setup ‘The following command installs Angular CLI globally: npm install -g @angular/cli 2. New Application The following command sets the prefix to “best.” practises --prefix best This command checks the available command list. ng new --help This command simulates the “ng new” command: ng new best-practises --dry-run 3. Lint for Format 9 The Lint command fixes code smells and corrects improper formatting. ng lint my-app --fix This next command shows warnings: ng lint my-app Hf you want to format the code, you can use the following command.ng lint my-app --format stylish Next, this command verifies the accessible list of commands. lint my-app --help 4, Blueprints Generate spec --spec Check whether the template will be a-ts file or not: inline-template Check whether the style will be in the-ts file or not --inline-style (-s) Create a directive: ng g d directive-name Create a pipeline: ng g p init-caps Create customer class in the models folder: ng g cl models/customer Creates a component without the need for the creation of a new folder. ng g ¢ my-component --flat true Assign a prefix: --prefixCreate an interface in the models folder: ng g i models/person Create an ENUM gender in the models folder: ng g € models/gender Create a service’ 5. Building Serving Build an app to /dist folder: ng build Optimize and build an app without using unnecessary code: ng build --aot Create a build for production: ng build --prod Specify serve with opening a browser: ng serve -o Reload when changes occur: ng serve --live-reload Serve using SSL ng serve -ssl6. Add New Capabilities ‘Add angular material to project: ng add @angular/material Create a material navigation component: ng g @angular/material:material-nav --name nav Components and Templates Components are the most fundamental Angular UI building pieces. component tree makes up an Angular app. Sample Component ts File import { Compon } from '@angular/core'; @Component ({ // component a’ selector: template’ styleUrls: » export class AppCompon: title = 'Hello World'; Component Attributes changeDetection The change-detection strategy to use for this component. viewProviders Defines the set of injectable objects visible to its view DOM children. moduleld The module ID of the module that contains the component. An Angularencapsulation ‘An encapsulation policy for the template and CSS styles, interpolation Overrides the default encapsulation start and end delimiters ({{ and }} entryComponents Asset of components that should be compiled along with this component. preserveWhitespaces True to preserve or false to remove potentially superfiuous whitespace characters from the compiled template Component Life Cycles ngOntnit Called once, after the first ngOnChanges() ngOnChanges Called before ngOnInit() and whenever one of the input properties changes. ngOnDestroy Called just before Angular destroys the directive/component. ngDoCheck Called during every change detection run. ngAfterContentChecked Called after the ngAfterContentinit() and every subsequent ngDoCheck() ngAfterViewChecked Called after the ngAfterViewlnit() and every subsequent ngAfterContentChecked() ngAfterContentinit Called once after the first ngDoCheck() ngAfterViewInit Called once after the first ngAfterContentChecked()Template Syntax {{user.name}} Interpolation - generates user name. property binding - bind image url for user to sre attribute Event - assign function to click event ser. sho
Register
Sample component: registerForm: FormGroup; submitted = false; constructor (private formBuilder: FormBuilder) { ngonInit() { this.registerform - this. formBuilder.group ({ firstName: [i {here default value}}, validators. required], lastName: ['', Validators. required] email: ['', [Validators.required, Validators.email]] password: a (Validators. required Validators.minLength(6)]]Ve // convenience getter for easy access to form fields get £() { return this.registerForm.controls; } onSubmit() { this.submitted = true; // stop here if form is invalid if (this.registerPorm.invalid) { return; alert (SUCCESS! ! Custom Validator for Reactive Forms Function: validateUrl (control: AbstractControl) { if (!control.value || control.value.includes('.png') || control.value.includes('.jpg')) {return null; return { validUrl: true }; Usage: this.secondFormGroup = this._formBuilder.group({ imageCtrl: ['', [Validators.required, this.validateUrl]] Multi-field validation: validateNameShire (group: FormGroup) { if (group) { if (group.get('isShirectrl').value —&& Igroup.get (*nameCtrl'). value. toString () .toLowerCase(). includes (' shire')) { return { nameShire : true }; return null; Multi-field validation usage:* this. firstFormGroup. setValidators (this. validateNameshire) ;Error handling