19. ROUTING
ROUTING - COMPONENT BASED
<Label pageTransition="flip" [nsRouterLink]="['/item', item.id]" [text]="item.name"
class="list-group-item"></Label>
same ol router link prefixed with ns
<router-outlet></router-outlet> where to render the content
20. ROUTING
ROUTING - PAGE BASED
<page-router-outlet></page-router-outlet> One per app
<ActionBar title="My App" class="action-bar">
</ActionBar>
Native navigation, back
Programmatic navigationflipToNextPage() {
this.routerExtensions.navigate(["/main"], {
transition: { name: “flip", duration: 2000, curve: “linear" }
});
} You change the entire page content
22. FORMS
TEMPLATE FORMS
<Button *ngIf="usernameModel.valid && passwordModel.valid"
(tap)="Save()"
text=“Save">
</Button>
Show ‘save’ button if both
fields are valid
ngForm is missing, which is
why we do this instead, per field
@NgModule({
imports: [
NativeScriptFormsModule,
]
})
<TextField required
#usernameModel="ngModel"
[(ngModel)]="username" >
</TextField>
<TextField required
#passwordModel="ngModel"
[(ngModel)]="password" >
</TextField>
Define the fields
23. FORMS
REACTIVE FORMS
constructor(
private formBuilder: FormBuilder) {
this.form = this.fb.group({
"name": ["", [Validators.required]]
});
}
Creating our form and fields
@NgModule({
imports: [
ReactiveFormsModule,
]
})
<StackLayout [formGroup]="form">
<TextField formControlName="name"></TextField>
<Label text="{{ form.get('name').value }}"></Label>
<Label text="Name value {{ form.get('name').status | json }}"></Label>
<Label text="Name status {{ form.value | json }}"></Label>
<Label text="Form status {{ form.status | json }}"></Label>
</StackLayout>
28. TEST
SET UP & RUN TESTING
tns test init Creates app/tests, not in release builds
Creates a sample test
Installs the nativescript-unit-test-runner
npm i @types/jasmine --save-dev Install TypeScript typings
tns test <ios/android>
Run the tests
tns test <ios/android> --watch Run tests on code change
Generates component + test
29. TEST
TEST THE COMPONENTimport { TestBed, inject } from '@angular/core/testing';
import { ProductDetailComponent } from './product-detail.component';
describe('a product-detail component', () => {
let component: ProductDetailComponent;
});
Suite
Set upbeforeEach(() => {
TestBed.configureTestingModule({
providers: [
ProductDetailComponent
]
});
});
Set upbeforeEach(inject([ProductDetailComponent], (ProductDetailComponent) => {
component = ProductDetailComponent;
}));
Testit('should have an instance', () => {
expect(component).toBeDefined();
});
31. NGZONE
CHANGE DETECTION NOT PICKED UP
Programmatically add view children
Using the FPS component
Changes are not picked up
Use NgZone
import {NgZone} from ‘@angular/core’;
@Component({})
export class SomeComponent {
constructor( private zone: NgZone ) {
}
doChange() {
this.zone.run(() => { this.prop = “new value”; })
}
}
33. SUMMARY
WE HAVE COVERED
▸ Set Up
▸ Components & Bindings
▸ Forms
▸ Routing
▸ Testing
▸ Http
▸ There are minor differences
▸ Nativescript truly embraces Angular