Skip to main content

19 posts tagged with "angular"

View All Tags

· 4 min read

This should help all the Angular developers out there to test yourself on the knowledge on Angular. I have listed down the concepts/questions from various sources such as Stackoverflow,Medium etc. Will be continuing this with 2 more posts.

1. What's new in Angular 6

  • Angular Elements - Angular Elements is a project that lets you wrap your Angular components as Web Components and embed them in a non-Angular application.
  • New Rendering Engine: Ivy - increases in speed and decreases in application size.
  • Tree-shakeable providers - a new, recommended, way to register a provider, directly inside the @Injectable() decorator, using the new providedIn attribute
  • RxJS 6 - Angular 6 now uses RxJS 6 internally, and requires you to update your application also. RxJS released a library called rxjs-compat, that allows you to bump RxJS to version 6.0 even if you, or one of the libraries you’re using, is still using one of the “old” syntaxes.
  • ElementRef - in Angular 5.0 or older, is that the said ElementRef had its nativeElement property typed as any. In Angular 6.0, you can now type ElementRef more strictly.
  • Animations - The polyfill web-animations-js is not necessary anymore for animations in Angular 6.0, except if you are using the AnimationBuilder.
  • i18n - possibility to have “runtime i18n”, without having to build the application once per locale

2. Difference between "Constructor" and "ngOnInit

  • The Constructor is a default method of the class that is executed when the class is instantiated and ensures proper initialization of fields in the class and its subclasses.

  • ngOnInit is a life cycle hook called by Angular to indicate that Angular is done creating the component. We have to import OnInit in order to use like this (actually implementing OnInit is not mandatory but considered good practice).

3. Difference between "declarations", "providers" and "import" in NgModule

  • imports makes the exported declarations of other modules available in the current module. It is also used to import supporting modules likes FormsModule, RouterModule, CommonModule, or any other custom-made feature module.
  • declarations are to make directives (including components and pipes) from the current module available to other directives in the current module. Selectors of directives, components or pipes are only matched against the HTML if they are declared or imported.
  • providers are to make services and values known to DI. They are added to the root scope and they are injected to other services or directives that have them as dependency.

 

A special case for **providers**are lazy loaded modules that get their own child injector. **providers**of a lazy loaded module are only provided to this lazy loaded module by default (not the whole application as it is with other modules).

Example : 

https://gist.github.com/sajeetharan/c3e872abd22fe0b43e7b8ae47289d018

4.Ahead of Time Compilation

 Angular Ahead-of-Time compiler pre-compiles application components and their templates during the build process.Apps compiled with AOT launch faster for several reasons

  • Application components execute immediately, without client-side compilation.
  • Templates are embedded as code within their components so there is no client-side request for template files.
  • You don't download the Angular compiler, which is pretty big on its own.
  • The compiler discards unused Angular directives that a tree-shaking tool can then exclude.
  • Also has small bundle size and faster load time.

5. Need of lazy loading modules in Angular.

Lazy loading is useful when the application size is growing. In lazy loading, feature module will be loaded on demand and hence application start will be faster.To load a feature module lazily we need to load it using loadChildren property in route configuration and that feature module must not be imported in application module.

· 2 min read

Hey guys, There has not been a  proper resource to have a checklist to make sure that applicaiton is flawless.I decided to write a simple tips on checklist items needed before you deploy your app to production.Following are a few Angular Code Review Checklists useful while doing a peer review of Angular code. Make sure to check these when you are building a production ready application

#1 - Code modularity  

Layered Code with good Modularity.

#2 - Component per file 

Each file must not contain more than one Component/Controller, etc.

#3 - Routing

Always configure routing with lazy loading

#4 - Shared resources at centralized location

Store images/language translations under assets

#5 - 3rd Party libraries 

 If you are integrating with any 3rd party libraries make sure to check for Security Flaws

#6 - Data security 

Use  Encryption of Sensitive Data

#7 - Offline data security

Consider security if you are storing Data in localstorage or session storage.

#8 - Cookies data and handwiring of secrets 

 Security Flaws

#9 - Don't use pipe/functions in the template

Calling function binding in the template will lead to  performance issue

#10 - Change detection + state management & reactive extensions

Use RxJS, NgRx/Store (or Redux)

#11 - Use javascript or typescript

 Stick to ONE & Avoid Hybrid

#12 - Don't use pipe/functions in the template

 Bundling, Chunking, Treeshaking, Minification, Uglification, Compression

#13 - ECMAscript compatibility - ES7

#14 - Angular Style Guide (Official Reference)

 https://angular.io/guide/styleguide

#15 - Async service

 Adequate and Appropriate use of it.

#16 - Hierarchical components,models,interfaces etc

#17 - Constants

No-Scattered Hard-Coded constants data but must be at one place.

#18 - Images,fonts and other static files

Place them in respective directories and not scattered across.

#19 - TSLint.json

To follow Angular Style Guide in order to run SonarQube or Codelyzer. Follow https://github.com/Angular-Projects-V-1-to-X/codelyzer

#20 - Finally to improve performance 

Refer the steps  - https://github.com/mgechev/angular-performance-checklist

Hope it helps all the developers out there.

· 4 min read

My first post for the year 2018 and i wanted to write something about a technology in which i have vast experience and passionate about. This blog will be very useful for those who want to start with angular for developing web applications.In this blog i am going to set up Angular 5 app using Visual Studio Code. I will be using Angular CLI for our sample application. The following are the steps needed to create your first angular application using angular5.

Steps

  • Install Node.js.
  • Install Angular CLI.
  • Design our  Angular App.
  • Launch  App in the browser.
  • Displaying our custom information on a page.

1. Install Node.js

In this approach, the first step is to install Node.js. If you have not installed nodejs in your machine visit the website and download according to your OS. You will see the following display.

Download and install the latest LTS version as per the configuration of your machine. The current version of Node LTS is 8.9.3. Installing node.js will also install npm (node package manager) on your system.

Next step is to make sure everything related to node is installed, To check the version of node and npm installed on your machine, run the following command in command prompt.

  • node -v 
  • npm -v

which will be as follows,

2. Install Angular CLI

We are done with the step1, next is to install angular cli. For those who are very new to angular , angular cli is a command line interface for angular applications. After installing node and npm, the next step is to install Angular CLI. Run the following command in a command window.

  • npm install -g @angular/cli

The above command will start Angular CLI installation on your machine. This might take bit of time  based on the internet connectivity.

 

 

 3. Design our  Angular App.

That's all folks, we have setup the necessary environment to start coding. Lets create our application in a folder named Angular5Demo. Run the following command to create a folder for our application.

  • mkdir Angular5Demo

lets navigate to our created folder, so run the following command.

  • cd Angular5Demo

we are in the same folder where our application resides, so lets create our template/structure for our application using angular cli. run the following command

  • ng new myFirstApp

if you have missed anything the following screen shows the steps and the output.

Lets see the code in visual studio code editor. Type code. in the command prompt.

  • code .  

Which will open up the code in the visual studio code. And that’s it.  You can see the following file structure in Solution Explorer. We have created our first Angular app using VS Code and Angular CLI.

4. Launch  App in the browser

 To launch the application and to start the web server run the following command 

  • ng serve 

After running this command, you can see that it is asking to open http://localhost:4200 in your browser. So, open any browser on your machine and navigate to this URL. Now, you can see the following page.

We'll leave the web server as it is while we look into the code of this application. Again open the code inside the path by typing the foolwoing command.

      code .

  1. D****isplaying our custom information on a page.

Now, Let's display a message saying "" using JSON data on the page instead of the message “Welcome to app!”. To achieve this, open /src/app/app.component.ts file. Change it as follows or according to the data you want to see on the page.

and HTML template as,

Change the value of title property to a JSON object as shown above.  You can see the updated web page as shown below.

With angular cli If the web server is running and you make any changes in the Angular application, then you don’t need to refresh the web page. It will automatically update itself as you save the application code file. You can now play with the appellation by using various JSON objects also by making requests.

That's it folks, we have learned about creating our first Angular 5 app using VS Code and Angular CLI. Please post your valuable feedback in comments section.

· 2 min read

Even though there have been plenty of tools/extension to check the typescript/javascript code, but there have not been any extension to validate the HTML code or related features to it. Angular Language Service was released to make the developers more productive and reduce the errors by offering better code completion.

This service is available in the market place for installation for the Visual Studio as well as other code editors that support Type Script. Errors can be detected at the time of code creation . Hints are also provided for code completion. This effectively allows us to use the Intellisence for variables defined and used in the template. The navigation service is provided to link properties and their definition. This extension provides a rich editing experience for Angular templates, both inline and external templates including:

 

  • Completions lists
  • AOT Diagnostic messages
  • Quick info
  • Go to definition

The language service is developed by the Angular core team. At the time of writing, it is ready to use in VSCode, Sublime Text and WebStorm.

Language service uses the Angular compiler for parsing our application and producing diagnostics. It decorates the TypeScript language service in and uses its logic again. The coolest feature about the service is that it is not coupled to a specific Angular version and can be used in any text editor and IDE as soon as there’s an available plugin.

More about the language service can be found in the ng-conf talk by Chuck Jazdzewski (the creator of the language service) “Using the Angular Template Language Service”.

Start using in your angular projects and be more productive. Cheers!