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 CodeMix and Angular IDE.  

MyEclipse licenses include access to CodeMix. 

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.

The wizard defaults to New Angular application, which creates an empty application. You also have the following options:

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. We could code more in the project first, but let’s dive in and see the application come up.

To start the server, go to the Servers view and expand Web Applications. Select the project, and then either right-click and select Start Server from the context menu, or click the Start button runServerIcon. A new tab opens in the Terminal+ view running “ng serve”. For more information on how Angular 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, right-click the project and select New>Component. The component is created in the src/app directory of your project.

Let’s create a component named RandomWords.

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.

To begin, right-click the project and select New>Service.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:

 

Let’s add a button that will trigger the service to get a new random word and add it to the list; we need to update src/app/app.component.html with the following:

 

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:

 

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

Appendix A: Working with Angular Workspaces and Libraries

Workspaces

Angular workspaces, introduced in Angular 6, are a great way to create multiple interdependent Angular applications and libraries.

To create an Angular workspace, use the new Project wizard and choose the No initial application option. The project created is very similar to a regular Angular application, but there is no source code. It does contain node_modules though, which will be shared with other projects and libraries within this Angular workspace. 

Libraries and Applications

To create a new Angular Application, or a new Library within a workspace, the easiest way is to invoke the new Application and Library wizards from the context menu of the Angular workspace—do not use the Angular project wizard.

Note: If you use the Run/Debug As>Angular Web Application action, or attempt to start the project from the Servers view, the Angular workspace’s default application is started. You can modify the default by editing the defaultProject property in the angular.json file, located in the root of the workspace.