Creating Your First Angular Application

Creating Your First Angular Application

Use this tutorial to create an Angular application from scratch. The example application supports getting and displaying random words from a remote web service. Exceptional Angular support is included in our Angular IDE, Webclipse Eclipse plugin and MyEclipse Java EE IDE.

Back to Learning Center

Download Angular IDE

Create the Project

The first step is to create a new Angular project using the New Angular Project wizard. To get started, select File>New>Angular Project. Type in a name for the project, and then click Finish to accept the default settings. The Angular IDE sets up a new project using the angular-cli.

angnewproject

See the Angular App in Action

After the project has been created, the project is listed in the Servers view. Let’s launch the Angular dev server. Yes, we could code more in the project first, but let’s dive in and see the application come up—because yes, it’ll actually update as you start to code it.

angstartserver


To start the server, go to the Servers view and expand Angular CLI. Select the project you created and click the Start button runServerIcon. A new tab opens in the Terminal+ view running “ng serve”. For more information on how Angular 2 applications run, see the Running the Angular Server.

Create a Random Words Component

To show a random word, you will first need to create a component that will display it. To create the component, select New>Angular>Component. The component is created in the app directory of your project.

Let’s create a component named RandomWords.

angnewcomponent

 

Once created, you’ll need to replace the contents of the component (src/app/random-words/random-words.component.ts) with the following source:

 

Note: @Input() words: Array<string>; means that the component accepts an array of string that can be set through “words” input property, we will come back to this later.

Then set the content of src/app/random-words/random-words.component.html to the following:

 

This will render a list with the content of the “words” property defined in random-words.component.ts. The ngFor directive is used to iterate through the items, in our case the “words” property. The current item in the iterable is being referenced by using “word”. We use {{word}} to print out the contents of “word”.

Update the Application to Show Some Words

AppComponent will be in charge of feeding the random words (through randomWords property) to RandomWordsComponent. For now let’s just use “hello” and “world”.

Replace or update the src/app/app.component.ts file to define some random words. Later you’ll update this to use a web service to get legitimately random words.

 

Now, let’s tell Angular to render the new RandomWords component and pass a set of words to that component that we defined above. Make the following changes to src/app/app.component.html:

 

Take a look at your browser! If everything was done correctly, you should now see hello and world shown for your application automatically. Congratulations, you’ve integrated your first component.

Get Random Words from a Web Service

OK, let’s make it interesting. You’ll now update the display of random words to come from a remote web service instead of hard coding it.

angnewservice


To begin, you’ll need to create a new service using the New>Angular>Service wizard. For more information, see Creating an Angular Service. Then, replace the contents in src/app/services/random-word.service.ts with the definition below:

 

Once created, you can then consume the service inside your AppComponent allowing dynamic generation of words loaded from the web service. Make the following changes to src/app/app.component.ts:

 

However, before this will all work, the NgModule will need to be updated for the application to allow everything to work together. Update src/app/app.module.ts with the following code:

 

And that’s it! If you followed all the steps correctly, you’ll now have up an application that will show random words.