Dimitry Karpenko
Java/Eclipse developer in MyEclipse and Webclipse teams.
Java/Eclipse developer in MyEclipse and Webclipse teams.
Right-click the empty area in Project Explorer/Project Explorer+, and choose New > Angular Project:
You’ll see the new Angular project wizard, where you can select Node.js, NPM and Angular CLI versions you want to use. For now, let’s enter project name “vehicles” and leave all versions default.
After that, you can either click “Finish” or “Next> “ and see a list of the commands which would be executed in local terminal to create your project.
In Terminal+ view you can see how commands are executed while creating your project. Also, Terminal+ is used while performing any Angular CLI commands – for creating new elements, starting server, etc. You can use it for entering your own commands as well.
Make newly created Vehicle class to be the following:
export class Vehicle {
id;
name;
type;
mass;
}
Right-click app folder again, choose New > Component:
In the newly opened wizard, enter element name vehicle-list:
Vehicle-list.component.ts file would be opened. It contains basic component’s infrastructure looking like following:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-vehicle-list',
templateUrl: './vehicle-list.component.html',
styleUrls: ['./vehicle-list.component.css']
})
export class VehicleListComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
All the basic component infrastructure is present here.
Click onto the link with the given suggestion, import statement for Vehicle class would be added to the top of the file.
import { Injectable } from '@angular/core';
const vehicles = [
{
id: 1,
name: 'Trailer - 1',
type: 'Truck',
mass: 40
},
{
id: 2,
name: 'An-2',
type: 'Plane',
mass: 5
},
{
id: 3,
name: 'LandCruiser 80',
type: 'Jeep',
mass: 2
},
];
@Injectable()
export class VehicleService {
private vehicles;
constructor() {
this.vehicles = vehicles;
}
getVehicles() {
return this.vehicles;
}
}
Now let’s go back to vehicles-list.component.ts, import VehicleService and make 2 more edits: add providers: [VehicleService] to @Component and change constructor to:
constructor(private vehicleService: VehicleService) {
this.vehicles = this.vehicleService.getVehicles();
}
We’re basically done with the component, let’s switch to UI. Open vehicle-list.component.html and replace its mock contents with our table.
<table class="tftable">
<tr><th>ID</th><th>Name</th><th>Type</th><th>Mass</th>
<tr *ngFor="let vehicle of vehicles">
<td>{{vehicle.id}}</td> <td>{{vehicle.name}}</td> <td>{{vehicle.type}}</td> <td>{{vehicle.mass}}</td>
</tr>
</table>
We do a conceptually simple thing here—create a table with a constant header and then iterate over vehicles list, creating a table row for each vehicle. A row is composed of cells with corresponding properties.
<app-vehicles-list></app-vehicles-list>Then go to Servers view (or open it if necessary), select your newly created project and click “Start Server”
You can monitor the startup process in Terminal+ view. Also, CLI would dispatch changes to project files and rebuild app immediately after changes are saved – this process is also reflected in Terminal+.
Open your browser (it’s highly recommended to use Chrome) and type localhost:4200 in its address bar. Here we go, application works!
<div *ngIf="vehicle">
<h2>{{vehicle.name}} properties</h2>
<table>
<tr>
<td><label>ID: </label></td>
<td>{{vehicle.id}}</td>
</tr>
<tr>
<td><label>Name: </label></td>
<td><input [(ngModel)]="vehicle.name" placeholder="name" /></td>
</tr>
<tr>
<td><label>Type: </label></td>
<td><input [(ngModel)]="vehicle.type" placeholder="type" /></td>
</tr>
<tr>
<td><label>Mass: </label></td>
<td><input [(ngModel)]="vehicle.mass" placeholder="mass" /></td>
</tr>
</table>
</div>
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { VehicleListComponent } from './vehicle-list/vehicle-list.component';
import { VehicleDetailsComponent } from './vehicle-details/vehicle-details.component';
@NgModule({
declarations: [
AppComponent,
VehicleListComponent,
VehicleDetailsComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
No compilation errors should happen after this. Warning could remain, but it doesn’t break down any functionality.
Now, we need to change our VehicleListComponent to handle selection. Let’s add a selectedVehicle field and a method onSelect to handle selection.
Also, in the html template we’ll need to add a tag for the details component. To make it work, we need to import VehicleDetailsComponent and add the corresponding directive. After given changes, vehicle-list.component.ts will look like the following:
import { Component, OnInit } from '@angular/core';
import { VehicleService } from '../model/vehicle.service';
import { Vehicle } from '../model/vehicle';
@Component({
selector: 'app-vehicle-list',
templateUrl: 'vehicle-list.component.html',
styleUrls: ['vehicle-list.component.css'],
providers: [VehicleService]
})
export class VehicleListComponent implements OnInit {
vehicles: Vehicle[];
selectedVehicle: Vehicle;
constructor(private vehicleService: VehicleService) {
this.vehicles = this.vehicleService.getVehicles();
}
ngOnInit() {
}
onSelect(vehicle: Vehicle) { this.selectedVehicle = vehicle; }
}
Next, let’s change the vehicles list template, vehicle-list.component.html. We need to add the click handler to each table row to call the corresponding selection method—(click)=”onSelect(vehicle)“. Also, let’s add a tag for the vehicle details component below our table:
<table class="tftable">
<tr><th>ID</th><th>Name</th><th>Type</th><th>Mass</th>
<tr *ngFor="let vehicle of vehicles"
(click)="onSelect(vehicle)">
<td>{{vehicle.id}}</td> <td>{{vehicle.name}}</td> <td>{{vehicle.type}}</td> <td>{{vehicle.mass}}</td>
</tr>
</table>
<vehicle-details [vehicle]="selectedVehicle"></vehicle-details>
Your changes should be automatically picked up and compiled; please, look at Terminal+. If you haven’t closed your browser, you don’t need to do anything – the browser should be updated automatically. Switch to it and you’ll see:
Perfect, it works. You can try editing any value under the “properties”, and the change will be immediately reflected in the table — nothing extra needed for it.
.selected { background-color: #CFD8DC !important;}
Let’s add hovering style too! It’s as easy as adding one line to the css file:
table.tftable tr:hover {background-color: #DDD; left: .1em;}
Great, our table looks much more interactive after this.
You can also see the component tree by clicking list icon, which is above the wand icon in CodeLive toolbox.
In our case a tree is pretty simple, but in case of real applications, which could consist of significant amount of components, such a tree with an ability to filter it could be very useful.
If you put your cursor near one of the items of this tree, you’ll see 3 actions allowing to open ts, html and css files for a given component. It could be very useful for those who want to investigate an application created by some other person.
Then go down to Servers view and click on “Debug application in Chrome” icon.
Chrome would be re-launched. Click some entry in Vehicles list to select it. The IDE would suggest you to switch to Debug perspective, after this you’d see the stuff well-known for Desktop or Android app developers – debug line mark, debug stack trace and variables view:
In the “Loaded JavaScript view” to the right of the source editor area you can see js files, which are loaded by the app.