Прив'язка Даних
Data Binding
В Angular є механізм прив'язки даних (Data Binding), з допомогою якого, ми можемо зв'язати деякі значення в файлах HTML і TS.
Прив'язка DOM елемента до значення компонента (одностороння).
<h1>Welcome {{name}}!</h1>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
name = 'Angular';
constructor() {
}
}
Прив'язка властивості HTML елемента до значення компонента (одностороння).
<p [innerHTML]="subName"></p>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
subName = 'Education';
constructor() {
}
}
<button [disabled]="isDisabled">LogIn</button>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
isDisabled = true;
constructor() {
}
}
Також можна використати результат функції.
<button [disabled]="getStatus()">LogIn</button>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor() {
}
getStatus() {
return true;
}
}
Прив'язка подій.
<button (click)="logIn()">LogIn</button>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor() {
}
logIn() {
console.log('login');
}
}
При виникненні події в фукцію ми можемо передати $event (подію що відбулася).
<button (click)="logIn($event)">LogIn</button>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor() {
}
logIn(event) {
console.log('event - ', event);
}
}
Завдання.
Додаємо title і subTitle в компоненту header.component.
Додаємо стилі для компоненти header.component.
<header class="app-header">
<h1>{{title}}</h1>
<p [innerHTML]="subTitle"></p>
</header>
Самостійна робота.
Створити компоненту footer.
Написати стилі:
footer завжди має бути притиснутим до низу сторінки. Якщо контент не помішається на сторінку, з'являється праворуч скрол і контент повинен штовхати footer в низ.
Last updated
Was this helpful?