Mpt Unit-1 Wordpress
Mpt Unit-1 Wordpress
UNIT: 1
Introduction to Angular Framework
❖ Why Angular :
Angular is an open-source, JavaScript framework written in TypeScript. Google maintains it, and its
primary purpose is to develop single-page applications. As a framework, Angular has clear advantages
while also providing a standard structure for developers to work with. It enables users to create large
applications in a maintainable manner.
❖ Development Environment:
• Nodejs
• Npm
• Angular CLI
• IDE for writing your code
Nodejs:
To install nodejs, go to the homepage, https://nodejs.org/en/download/ of nodejs and install the
package based on OS.
To check if nodejs is installed on system, type node -v in the terminal.
C:\>node –v
Npm:
Once nodejs is installed, npm will also get installed along with it.
To check if npm is installed or not, type npm –v in the terminal as given below. It will display the
version of the npm.
C:\>npm –v
Angular CLI:
Refer https://cli.angular.io/ of angular to get the reference of the command.
Type npm install –g @angular/cli in your command prompt, to install angular CLI on your system.
Once installation done check the version using below command −
ng version
It will display version details of angular - cli as well version of others packages.
✓ To create a new workspace and initial starter app. Run the CLI command ng new and provide the name
my-app, as shown here:
ng new my-app
✓ The ng new command prompts you for information about features to include in the initial app. accept the
defaults by pressing the Enter or Return key.
✓ The Angular CLI installs the necessary Angular npm packages and other dependencies. This can take a
few minutes.
✓ The CLI creates a new workspace and a simple Welcome app, ready to run.
ng serve --open
✓ The ng serve command launches the server, watches your files, and rebuilds the app as you make
changes to those files.
✓ The --open (or just -o) option automatically opens your browser to http://localhost:4200/.
✓ If your installation and setup was successful, you should see a page similar to the following.
App.component.html file:
<div>
<div class="center-screen">
<h1>{{text1}}</h1>
</div>
</div>
App.component.ts file
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'practical1';
text1 = "Hello World"
}
RootHTML--index.html
The Entry Point--main.ts
Main Module--app.module.ts
Root Component-AppComponent
❖ Creating a Component:
Now, if we go and check the file structure, we will get the student-component new folder created under
the src/app folder.
By default, this command creates the following:
A directory named after the component
A component file, <component-name>.component.ts
A template file, <component-name>.component.html
A CSS file, <component-name>.component.css
A testing specification file, <component-name>.component.spec.ts
Where <component-name> is the name of component.
Syntax:
The ngModel directive needs to be enabled for two-way data binding. The FormsModule needs to be
added in imports[] array in the AppModule, because the ngModel directive depends upon FormsModule
in angular/forms package.
Example:
• Add the following code in app.module.ts:
<h2>Two-way Binding</h2>
<input [(ngModel)]=" Name" /> <br/><br/>
<p> {{Name}} </p>
Output:
Example:
<img [src]=”imgUrl” />
Property binding has an advantage of shorter and cleaner code over string interpolation.
For simply displaying some dynamic data from a component on the view between headings like h1, h2,
p etc, String interpolation is preferred.
If the field value in the component of the String Interpolation and Property binding changes, Angular
will automatically update the DOM. But since both the String Interpolation and Property binding
represents one-way data binding, thus any changes in the DOM will not be reflected in the component.
Example:
• Add the below code in app.component.ts:
Output:
The myClick() method in this example is called from the component when the button is clicked.
Ex:
In app.component.html:
In app.component.ts:
}
}
Cache commands
clean
ng cache clean
To write clean code, there are some key points here:
Write readable code. Focus on writing code that is easy to understand.
Separation of concerns. Encapsulate and limit logic inside your components, services and directives.
Each file should only be responsible for a single functionality.
Utilize TypeScript. TypeScript provides advanced autocompletion, navigation and refactoring. Having
such a tool at your disposal shouldn’t be taken for granted.
Use TSLint. TSLint checks if TypeScript code complies with the coding rules in place. Combined with
Prettier and Husky makes for an excellent workflow.
RxJS in Angular. RxJS comes packaged with Angular, it’s to our great advantage to make the most of
it.
Clean up imports with path aliases. We can clean up our imports considerably by using path aliases to
reference our files.