@Component({
selector: 'saUiDatepicker',
template: `<input type="text" #input class="form-control datepicker" placeholder="{{placeholder}}" [(ngModel)]="value" (blur)="onBlur()">`,
providers: [CUSTOM_UI_DATEPICKER_CONTROL_VALUE_ACCESSOR]
})
export class UiDatepickerComponent implements OnInit, ControlValueAccessor {
private innerValue: any = '';
private onTouchedCallback: () => void = () => { };
private onChangeCallback: (_: any) => void = () => { };
get value(): any {
return this.innerValue;
};
set value(newValue: any) {
if (newValue === this.innerValue) return;
this.innerValue = newValue;
this.onChangeCallback(newValue);
}
@Input() options: any;
@Input() placeholder: any;
@ViewChild('input') el: ElementRef;
onBlur() {
this.onTouchedCallback();
}
writeValue(value: any) {
if (value !== this.innerValue) {
this.innerValue = value;
}
}
registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
ngOnInit() {
let options = $.extend(this.options || {}, {
onSelect: (selectedDate) => {
this.onChangeCallback(selectedDate);
}
});
System.import('jquery-ui').then(() => {
const datePicker = $(this.el.nativeElement).datepicker(options);
if (this.value) {
datePicker.datepicker("setDate", this.value);
}
});
}
}
<form #templateForm="ngForm" (ngSubmit)="submit(templateForm.value)">
<saUiDatepicker name="data" [(ngModel)]="data" [options]="{...}"
placeholder="Выберите дату"></saUiDatepicker>
<button type="submit">Submit</button>
</form>
<form [formGroup]="reactiveForm">
<saUiDatepicker formControlName="data" [options]="{...}"
placeholder="Выберите дату"></saUiDatepicker>
<pre>{{ reactiveForm.value | json }}</pre>
</form>
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.reactiveForm = this.fb.group({
data: ''
});
}
<test-input></test-input>
<test-select></test-select>
<test-input [value]="data"></test-input>
import { Component, Input } from '@angular/core';
@Component({
selector: 'test-input'
})
export class TestInputComponent {
// Тут значение принимается!
// Тип значения должен стоять тот, который ожидается
// @Input() - это то что называется декоратором, он служит для передачи значений между директивами
@Input()
value: any;
}
new Intl.NumberFormat([locales[, options]])
import { CurrencyPipe } from '@angular/common';
const map = {
'RUB': 'ru-RU',
'JPY': 'ja-JP'
}
@Pipe({name: 'localizedCurrency'})
export class LocalizedCurrencyPipe implements PipeTransform {
transform(value: any, currencyCode: string = 'USD',
symbolDisplay: boolean = false, digits: string = null): string {
return new CurrencyPipe(map[currencyCode]).transform(value, currencyCode, symbolDisplay, digits);
}
}
template
. Переписать можно вот так:<template ngFor let-row [ngForOf]="review.penalty">
<tr *ngIf="row.sum > 10">
<td>{{row.name}}</td>
<td>{{row.sum | number:'.2'}}</td>
</tr>
</template>
<ng-container *ngFor="let row of review.penalty">
<tr *ngIf="row.sum > 10">
<td>{{row.name}}</td>
<td>{{row.sum | number:'.2'}}</td>
</tr>
</ng-container>
<template ngFor let-row [ngForOf]="review.penalty">
<tr *ngIf="row.sum > 10">
<td>{{row.name}}</td>
<td>{{row.sum | number:'.2'}}</td>
</tr>
</template>
Как создать вложенность ссылок?
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private userService: UserService) {}
canActivate(route: ActivatedRouteSnapshot, state:RouterStateSnapshot): Promise<boolean>|boolean {
return this.userService.isAuth;
}
}
{
path: 'logout',
component: LogoutComponent,
canActivate: [AuthGuard]
},
{
path: 'login',
component: LoginComponent,
canActivate: [GuestGuard]
},