Design a site like this with WordPress.com
Get started

Manage data with Angular JS

In this tutorial we will create simple hello world example and pass data between typescript and html.We will also see *ngFor and *ngIf

Prerequisites

Angularj JS new version is using nodejs for setting up application and for running applicaion you will also need any code editor tool.

For current setup we are using visual code studio

Kindly down load latest version of nodejs and visual code studio on your machine and complete setup

Node Installation

1 Go to this site and download latest version of node js

https://nodejs.org/en/

2) Once installed open command prompt and type “node”

you will be able to see version on node js that means nodejs installed

Install Visual Studio

1 Go to this site and download latest version of

https://code.visualstudio.com/

2. Once installed on windows go to search menu and type visual you will be able to see its installed like below screenshot

3.Open Application and you will be able to see below screen

4) Select folder from file menu for your project

5) Open Terminal from Terminal option

Install Angular cli

Run below command

  npm install -g @angular/cli

once its installed type below command in terminal to verify installation

Create a workspace and initial application

Run below command

  ng new my-app

it will ask for angular routing =>select yes

then select css as style sheer language like below screenshot

Press Enter and it will generate your project

in below diagram 1) Creating html and components from schematics 2) DOwnloading required packages you can see under node_modules folder

Run the application

Run below command

traverse to application folder

 cd my-app  

run below command to start and open application

ng serve –open

this command will compile app and launch it with webpack on http://localhost:4200/

Congrats your first application is running and working fine

Creating Hello World

In angular data binding is happening between html and component classes .

Here we will declare one variable in component file and same will be visible on UI .

  1. Delete everything from app.component.html.

2. Declare variable in app.component.ts

app.component.ts

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'my-app';
  message = 'Hello World';
}

3. Now add binding between UI and component with scriplet tag

app.component.html

<div>
<h1>{{message}}</h1>
</div>

4. Run below command to compile app

ng serve

5. Open browser and enter http://localhost:4200/

Edit app.component.ts

Your application is running and rented “Hello World” in browser. Same way you can initialize variables and pass data from component to UI.

Create Array and String in

Here we have declared title as string and cars as array. Internally typescript will understand data types

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Display data of Cars';
cars = [ 'city' , 'nexon', 'scorpio' ];
}

Edit app.component.html

Map variables in html using below code

with {{ }} we have mapped title in below html ..

For showing array we have to iterate through it so we have used “let”,which will help to get value one by one from array and store in “c” variable,

We can now simply prin

<div>
<h1> {{ title }} </h1>
<ul *ngFor = "let c of cars ">
<h1> Car Name:{{c}} </h1>
  <li *ngIf = " c == 'nexon' ">
    <h2 style="color: brown;">
  This is new car:  {{c}}
  </h2>
  </li>
</ul>
</div>

Once you save your changes just run below command

ng serve --open

Output:

Below output will get printed on HTML page..For loop will rotate array and if will add conditions to it.

GIT REPO:

https://github.com/letsblogcontent/AngularJS/tree/master/DisplayData

Advertisement

Publish Build Scans

You can publish your build and details that will provide with you with the insights of what happened when you ran the build. The build are published to a centralized server.

How to publish

When you run the gradle build, pass the parameter –scan with the build. This will deploy the build to the centralized server.

When requested, accept the terms by typing “yes”

The build will also display a url where you can see details as below of your build. Follow the instructions to view details

Gradle – Task

A Gradle build is made up of tasks. Each task represents a set of instructions to gradle for execution.

A task might consist of compiling classes, copying files from source derectory to destination.

A simple task is defined below.

task hello {
   doLast {
      println 'helloworld'
   }
}

put the above code in a build.gradle file and execute gradle hello, it should print helloworld.

The “doLast” keyword tells the gradle to execute the statements within doLast at the last of the task execution. Similarly there is “doFirst“. As the name suggest, the doFirst will execute before any other sub task is executed within a task.

if there are multiple doLast, the last doLast will be executed in then end and then the prior and so on.

if there are multiple doFirst, the last doFirst will execute first and then prior one and so on.

You can also view the task you created by running gradle task –all under “Other Tasks“.

Task dependencies

It is possible to add dependencies in a task. so a task B might be dependent on the execution of task A. To do this, you have to add set the dependsOn attribute of a task

e.g.

task taskA {
   println 'taskA'
}
task taskB(dependsOn: 'taskA') {
   println "taskB"
}

The above build.gradle will always execute taskA before taskB as we have mentioned the dependency in the build file. So if you run the task taskB (gradle taskB) it will execute taskA first and then taskB

Null Pointer Exception

Null Pointer Exception or NPE is one of the most common exception that anyone of us have encountered as a development engineer.

It is usually thrown when we try to access a method on a reference variable for which a value is evaluated to null.

for e.g

Employee e = getEmployee(123);
e.getEmployeeName();

The above code can throw a NPE. The above code assumes that the “getEmployee” method will always return a value but it may happen that it wont and return null;

In such a case the code at line 2 throws NPE as we are trying to access “getEmployeeName()” on a null value.

Mind you, the NPE is a runtime exception and the compiler will not complain that you have not handled a null check.

How to Resolve

In most cases a simple null check resolves the problem. Like in the above example an If condition will avoid NPE

Employee e = getEmployee(123);
if(e!=null)
e.getEmployeeName();

A code should be written to identify such conditions and handle them wherever necessary. As NPE is a runtime exception, if the code is not handled for null object access, the program might very well terminate abruptly for a small null check.

Angular JS Application Configuration Files

Angular JS project is having multiple files in it.In this post we will understand their types and importance of them.

First look at project structure in below image then will see all files in detail

In Above image there are two types of files

  • Workspace configuration files
  • Application project files

These files is having different kind of purpose to help angular js application to run ,compile and test successfully

Application configuration files


Once workspace can have multiple projects in it but all will have application configuration files which will be used to maintain that application.

There are three types of application configuration files

Source Files

assets/
environments/
favicon.ico
index.html
main.ts
polyfills.ts
styles.sass
test.ts
app/

Configuration

browserslist
karma.conf.js
tsconfig.app.json
tsconfig.spec.json
tslint.json

End-to-end test

•app.e2e-spec.ts •app.po.ts •protractor.conf.js •tsconfig.json

Below are application configuration files

Source Files

assets/

This folder holds details about image and other asset files


environments/

This folder having list of all files related to environment ,by default one dev and one prod environment related file will be created by default.


favicon.ico

This file holds all details about an icon to use for this application in the bookmark bar


index.html

This is starting point of all angular js aplication by default


main.ts

This is entry point for your angular application which holds details about bootstrapping module to start application.


polyfills.ts

This file will help for running angularjs application in different browsers.This will have pollyfill scripts which will be used by browsers.


styles.sass

This file holds details about lists of CSS files that supply styles for a project.



test.ts

This is entry point like main.ts but its getting used for testing angularjs application.


app/

This folder having all component,service,directive,routing and pipes in it specific to application. by default AppComponent will get created by angularjs while creating project. Once project is created you can add your components ,directives or services according to your need.


Configuration Files

browserslist

This file is used by the build system to adjust CSS and JS output to support the specified browsers


karma.conf.js

This is used for angularjs testing application .Angularjs using karma as framework for testing and using jasmine as framework.


tsconfig.app.json and tsconfig.spec.json

This file hold TypeScript configuration details which will helpful to run typescript project and follow all standards ,it also specifies the root files and the compiler options required to compile the project.

For more details please refer : https://www.typescriptlang.org/docs/handbook/tsconfig-json.html


tslint.json

This files holds all linting details which will he used by ng lint command to check format.This can be used to analyse typescript code if it is as per standard or not.

For more details please refer:https://palantir.github.io/tslint/


End-to-end test

app.e2e-spec.ts

This file is running end to end test for application


app.po.ts

This is default pojo getting created for end to end to test by angularjs


protractor.conf.js

This file holds configuration for protractor which helps to test your application end to end.

This file is having all details about ur,framework which is getting used by protractor and location about test case..etc


tsconfig.json

This file hold TypeScript configuration details which will helpful to run typescript project and follow all standards ,it also specifies the root files and the compiler options required to compile the project.

For more details please refer : https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

All these files are getting used to manage configuration of entire application.

Angular JS Project Structure

Angular JS project is having multiple files in it.In this post we will understand their types and importance of them.

In Above image there are two types of files

  • Workspace configuration files
  • Application project files

Workspace configuration file

workspace are having below files

  1. .editorconfig
  2. .gitignore
  3. README.md
  4. angular.json
  5. package.json
  6. package-lock.json
  7. node_modules/
  8. src/
  9. tsconfig.json
  10. tslint.json

Application project files

There are again different types of files which are getting used to code,test ,run and deploy applications in angular js

Below mentioned folders are clearly mentioned in above image and it also has other files in it

  1. source
  2. configuration
  3. End-to-end test

We will cover them in details in upcoming posts which will help you to get more details about configuration files .









Angular JS Workspace Configuration Files

Angular JS project is having multiple files in it.In this post we will understand their types and importance of them.

First look at project structure in below image then will see all files in detail

In Above image there are two types of files

  • Workspace configuration files
  • Application project files

These files is having different kind of purpose to help angular js application to run ,compile and test successfully

Workspace configuration files


Below are workspace configuration files

.editorconfig

This file holds details used by editor which helps to maintain consistent coding styles across different editors and developers


.gitignore

This file having list of all files and folder which needs to be excluded from angularjs project while checking in to repostitory


README.md

This file is acting as document for angular js applicaiton which will have alld etails about application and its usability.


angular.json

This file holds all configuration details to angular js application whcih will help to run different commands like build,run ..etc


package.json

This is having all below details

  1. scripts
  2. Dependencies use for application
  3. Application basic details
  4. Dev dependencies which is used for development only.

package-lock.json

This holds details about version information for all packages installed into node_modules


src/

This folder is having all application source which will get compiled and run.


node_modules/

This folder holds all dependencies related to your application.When you run npm install all dependencies will get downloaded to this folder.



tsconfig.json

This file hold TypeScript configuration details which will helpful to run typescript project and follow all standards ,it also specifies the root files and the compiler options required to compile the project.

For more details please refer : https://www.typescriptlang.org/docs/handbook/tsconfig-json.html


tslint.json

This files holds all linting details which will he used by ng lint command to check format.This can be used to analyse typescript code if it is as per standard or not.

For more details please refer:https://palantir.github.io/tslint/

All these files are getting used to manage configuration of entire workspace .One workspace can have multiple projects or multiple libraries in it .

Types of Project you can Create with AngularJS

Types of angular js projects

If you don’t know angular project creation then please refer below link first

hello-world-with-angular-js

1 Library project


With Angular Js you can create libraries which can be easily imported in other angularjs projects and used.

This gives you advantage to reuse code or put boilerplate code in common library that can be used repetitively in different project

Below Command is getting used to create library in angular js

ng generate library my-lib

Below is example of structure of library project

2. Multiple Projects


Angularjs given you option to create workspace which can hold multiple small projects including libraries which give you tremendous advantage to manage your source code efficiently.

ng new my-workspace --createApplication="false"
cd my-workspace
ng generate application my-first-app
ng generate application my-second-app
ng generate library my-lib

As shown in above command you can create workspace first

ng new my-workspace –createApplication=”false”

then go to workspace

cd my-workspace

Now create your first application in workspace

ng generate application my-first-app

Now create your Second application in workspace

You can also create library in workspace with below command

ng generate library my-lib.

Below is project strucutre of workspace

3. Angular JS Application


This is most common way to use angularjs most of beginners straight away creates project with angular with this type of project.

This will create workspace with your angularjs project and all necessary things will be configured so you can directly start coding with this appraoch.

Lets see how to create it

Create a workspace and initial application

  ng new my-app

Run the application

cd my-app  
ng serve --open

Below is project structure for this kind of project

Java Gradle Dependency management

One of the key feature of the build is managing the dependencies in the project. If we have to use a third party library, we declare that library in the build file i.e. build.gradle and then expect the build tool to download the library and put in the classpath of our project for consumption.

Let say we want to use google guava in our project, so we add the below dependency in the build.gradle file.

compile group: 'com.google.guava', name: 'guava', version: '29.0-jre'

The above line will put the guava library in our project classpath and make the library available for us to use.

Dependency scope basically tells the gradle tool of our intention of when we want the library to be available. Lets go through one – by one.

  • Implementation
    It is used to declare dependencies that we don’t want to expose to consumers compile time. So it means that if we do not want the dependencies in our project to be exposed to the consumers of our project, we declare the dependency as implementation. The is the default scope and is an improvement over the compile scope. The compile scope is deprecated and will no longer be available in latest build scrips. So this scope avoids pollution of the transitive dependencies which will happen otherwise with compile scope.

  • api
    Use the api configuration do declare dependencies that are part of our API, i.e. for dependencies that we explicitly want to expose to our consumers. So if you want the dependency to be the part of the api for the consumers, then you declare the dependency as api.

  • compileOnly
    As the name suggests the dependencies which are supposed to be available only at compile time, such dependencies are marked compileOnly. E.g. of such a dependency is lombok. The lombok library is not required at runtime as it creates the required class files at compile time based on the annotations.

  • runtimeOnly
    Dependencies available only at the runtime are declared runtime dependencies.

testImplementation / testCompileOnly / testRuntimeOnly
testImplementation dependencies are only available during compilation and runtime of tests.

testCompileOnly dependencies are only available during compilation of tests and not at runtime.

testRuntimeOnly dependencies are only available during runtime of tests and not at compile time.

Create a new Gradle Project – Intellij

Pre-requisite

  1. IntelliJ
  2. Gradle enabled in intelliJ

Steps

  1. Open Intellij
  2. Navigate to File -> New Project and it will show below window
  3. Select Gradle project as shown below, select Java installation location if not already set and click on next.

  4. As shown below, provide project name, location, group id and artifact id and click on finish
  5. This should create your new Gradle project. The build.gradle will look below.
  6. Next will create a class and run the project to display Hello World. It is pretty much self explanatory from here on. The gradle project creation is completed at the 5th step already. The below steps are just to try and create a new class and make it run.

    Create new class


    print hello world



    Right Click on the HelloWorld class and select run to run the program.