Create pages with Angular
Create pages with Angular 1Create child components
I could add a child component to top-page component like below.ng g component top-page/product-rankingproduct-ranking folder was made under the top-page folder.
src
L app
L top-page
L product-ranking
L product-ranking.component.css
...
Base size
I thought, the important thing was the base size of component was affected by the parent component.Although there had been some parent elements, the child component's size ignored them.
For example, I generated two components on the same directory, and made parent-child relationship by HTML template.
src
L app
L top-page
L product-ranking // component
L product-ranking-block // component
L product-cell // component
product-ranking.component.html
<div id="product-ranking-area">
<div id="product-ranking-frame">
<div id="product-ranking-group">
<app-product-ranking-block></app-product-ranking-block>
</div>
</div>
</div>
product-ranking-block.component.html
<div class="product-ranking-block" style="width:100%; height:100%;">
<div class="product-ranking-block">
<app-product-cell class="product-cell"></app-product-cell>
</div>
</div>
product-cell.component.html
<div id="product-cell-frame" style="width:100%; height:100%;">
</div>
product-ranking-block class element's size and product-cell-frame class element's one were same.
So I thought the best way was making file structures same as the HTML template structure.
Use Pikaday
Because I wanted to use Datepicker.If I could only support Chrome and Firefox, I could use input type=date.
But IE11 didn't support it, so I had used Pikaday.
Pikaday - GitHub: A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS
Maybe I should use other Datepicker what was made for Angular.
But because I had already used Pikaday, so I tried this time.
Install
I installed Pikaday and moment.js.npm install --save moment pikaday @types/pikaday
When I had used Pikaday with no JS framework, I had to added Pikaday.js and Pikaday.css.
About Pikaday.js, I just imported from using Datepicker component class.
top-page.component.ts
import { Component, OnInit } from '@angular/core';
import * as Pikaday from 'pikaday';
...
export class TopPageComponent implements OnInit {
...
ngOnInit() {
let picker = new Pikaday({
field: document.getElementById('datepicker')
});
}
}
top-page.component.html
<input type="text" id="datepicker">
<app-product-ranking></app-product-ranking>
But how about CSS?
After I tried, I decided import it from style.css.
style.css
@import '../node_modules/pikaday/css/pikaday.css';
...
Customize view
I wanted to localization, and some customizing.top-page.component.ts
import { Component, OnInit } from '@angular/core';
import { CustomDatePicker } from '../custom-datepicker';
...
export class TopPageComponent implements OnInit {
...
ngOnInit() {
let picker = CustomDatePicker.getPikaday('datepicker');
}
}
custom-datepicker
import * as Pikaday from 'pikaday';
import * as moment from 'moment';
export class CustomDatePicker{
public static getPikaday(elementName: string): Pikaday{
const today = new Date();
const yearTo = today.getFullYear() + 1;
return new Pikaday({
field: document.getElementById(elementName),
firstDay: 1, // start from Monday
minDate: new Date(moment(today).format("YYYY-MM-DD")), // from today
maxDate: new Date(`${yearTo}-12-31`), // to after one year
yearRange: [today.getFullYear(), yearTo],
yearSuffix: '年', // show like "2019年"
showMonthAfterYear: true, // show like "2019年 四月"
/* localize to Japanese */
i18n: {
previousMonth : '前月',
nextMonth : '次月',
months : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
weekdays : ['日曜','月曜','火曜','水曜','木曜','金曜','土曜'],
weekdaysShort : ['日','月','火','水','木','金','土'],
}
})
}
}
https://github.com/Pikaday/Pikaday/blob/master/pikaday.jsData bindings
Because I used Angular, I wanted to use Data bindings.top-page.component.html
<input type="text" id="datepicker" [(ngModel)]="dateText">
<div>{{dateText}}</div>
<app-product-ranking></app-product-ranking>
top-page.component.ts
import { Component, OnInit } from '@angular/core';
import { CustomDatePicker } from '../custom-datepicker';
...
export class TopPageComponent implements OnInit {
dateText: string = "";
...
ngOnInit() {
let picker = CustomDatePicker.getPikaday('datepicker');
}
}
But I got nothing.
If I input some words directory in the textbox, Data bindings worked.
Because the change event could fire, so I could manually resolve it.
top-page.component.html
<input type="text" id="datepicker" (change)="onValueChanged()">
<div>{{dateText}}</div>
<app-product-ranking></app-product-ranking>
top-page.component.ts
import { Component, OnInit } from '@angular/core';
import { CustomDatePicker } from '../custom-datepicker';
...
export class TopPageComponent implements OnInit {
dateText: string = "";
...
ngOnInit() {
let picker = CustomDatePicker.getPikaday('datepicker');
}
onValueChanged(){
let inputText = document.getElementById('datepicker') as HTMLInputElement;
this.dateText = inputText.value;
}
}
But why the Data bindings wasn't work?
Next time, I will search the reason.
コメント
コメントを投稿