Mobile Programming 3 Try Angular
Mobile Programming 3 Try Angular
angular
Event and property binding
Input information
[(ngmodel)]
Person-input.component.html
• <label for="name">Name</label>
• <input type="text" id="name" #personNameEl>
• <button (click)="onCreatePerson(personNameEl.value)">Create</button>
TO
• <label for="name">Name</label>
• <input type="text" id="name" [(ngModel)]="enteredPersonName">
• <button (click)="onCreatePerson()">Create</button>
•
App.module.ts
• import { FormsModule } from '@angular/forms';
.
.
.
person-input.component.ts
After pass $event (reserve value in angular to pass data for event carries)
• <app-person-input (personCreate)="onPersonCreated($event)"></app-
person-input>
• <app-persons [personList]="persons"></app-persons>
•
app.component.ts
• persons: string[] = ['Max', 'Manuel', 'Anna'];
•
onPersonCreated(name: string) {
• this.persons.push(name);
• }
>Above assignment 4
Routing one html page 2 components
• Create a module for routing
app-routing.module.ts
note: persons change to yourname
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
App module.ts
import { AppRoutingModule } from './app-routing.module';
.
.
.
imports: [BrowserModule, FormsModule, AppRoutingModule],
Services
• Class that can interact with other component
Person.component.html
<ul>
<li *ngFor="let person of personList"
(click)="onRemovePerson(person)">
{{ person }}
</li>
</ul>
App.component.html
• <a routerLink="/">Person List</a> | <a routerLink="/input">Input</a
>
• <hr>
• <router-outlet></router-outlet>
•
person.service.ts
• Create this file
person.service.ts
import { Injectable } from '@angular/core';
addPerson(name: string) {
this.persons.push(name);
console.log(this.persons);
}
removePerson(name: string) {
this.persons = this.persons.filter(person => {
return person !== name;
});
console.log(this.persons);
}
}
Because array already in service the app.component .ts become
@Component({
selector: 'app-persons',
templateUrl: './persons.component.html'
})
personList: string[];
// this.personList = prsService.persons;
// this.personService = prsService;
ngOnInit() {
this.personList = this.prsService.persons;
onRemovePerson(personName: string) {
this.prsService.removePerson(personName);
}
App.component.html
<a routerLink="/">Person List</a> | <a routerLink="/input">Input</a>
<hr>
<router-outlet></router-outlet>
Above assignment 5