Introduction
Angular 2 is a framework for building web or mobile applications in HTML and Javascript or Typescript. It’s the right choice of framework to use for your web or mobile applications.
Table of contents
Today, we’re going to discuss a couple of things that may help you boost your development process:
- The advantages of using angular2,
- Basic concepts to start a new project,
- How an angular2 project is structured and how each element works.
Characteristics
TYPESCRIPT LANGUAGE
What is typescript? Well, Typescript is an ES6 superset, which means it includes every function EM6 has but also has its own extra functions. This makes possible for you to mix JavaScript code and typescript code without a problem. Typescript is easier to use; and makes your code more understandable. You are not obligated to use this language when programming in Angular2, and you are still able to code in ES5 and ES6, although I recommend you to use it because it will be easier for you to find documentation and examples about it.
ROUTING
Routing is easier than ever in Angular2. It has an intuitive configuration thanks to @RouteConfig in which routes are defined, every route has a component linked to it and an ID, which is used to create the link. Data can be passed through the link.
INTEGRATING OTHER TECHNOLOGIES
Angular2 is not a completely self-sufficient framework, but one of its biggest strengths is that other component creating technologies can be integrated easily into it. For example, with the help of Ionic we can use Angular2 to create hybrid mobile apps. This makes Angular a very useful and powerful tool.
DATA BINDING
Data binding is the connection between what the user sees and the logic of the application
In Angular1 data-binding took a lot of time to process on the browser. Now in Angular2 the developer can control and direct easily the data flow making the application faster.
The types of data binding in angular2 are this:
- Interpolation
- One-way binding
- Two-way binding
- Event binding
ANGULAR CLI (COMMAND LINE INTERFACE)
This tool is provided by the Angular team, starting with Angular2. It makes the process of starting a project easier since only command folders and files that are necessary for your project are created.
To be able to use the CLI first you need to install it with this command:
npm install -g angular-cli
It is important to say that to use the npm and the ng commands you must have installed Nodejs on your computer.
To create a new project with its default folders and files you need to use the next command:
ng new project-name
The Angular CLI has a server included so you can run the project; the command to use the server is this:
ng serve
This command will launch the web server, and in the link to the server will appear on the console.
The Angular CLI contains a significant amount of useful commands; in this section, I just showed you a few of them.
The Structure of Angular2
In Angular2, everything works around components. Components have templates, classes and metadata associated with them, in this section we will learn about all of these elements that make up a component. But first, what is a component and how do they work?
COMPONENTS
Components are HTML tags that we use to carry out the necessary functions for our project.
We define a component like this:
@Component({ moduleId: module.id, selector: 'tag-name', templateUrl: 'project-name.component.html', styleUrls: ['project-name.component.css']})
TEMPLATES
The selector is the name we give to the tag so we can use it later on in the HTML file, the template URL refers to the view associated with the component and the styleUrls refers to the stylesheet associated with the component.
To call the component in the HTML file all you need to do is write the text we assigned to the selector like it is a normal HTML tag, like this:
<tag-name>Loading...</tag-name>
METADATA
A component for Angular2 will be at first class until you specify Angular that it is a component, we can do this using metadata. Let us use the previous example:
@Component({ moduleId: module.id, selector: 'tag-name', templateUrl: 'project-name.component.html', styleUrls: ['project-name.component.css']})
In this code, all the configurations we make are metadata (selector, templateUrl and styleUrls). The metadata tells Angular where to get the files or tools you specify for the component.
Conclusion
Angular2 is a powerful tool for every programmer whether you are a beginner or an expert, it makes easier to organize and code your project. You can integrate other technologies pretty easy! So, what are you waiting for? Angular2 is the tool for you!
In ClickIT we use this framework to give a quality service to our clients as Ticket To Go , implementing the best practices and integrating the top technologies at the moment.