diff --git a/src/app/app.component.html b/src/app/app.component.html index 5c1484e..17ee947 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,2 +1,2 @@ - -

目前文字尺寸:{{ fontSize }} pt

+ + diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 35a3242..3100cb4 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,28 +1,8 @@ -import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'; - -import { FontSizeComponent } from './font-size/font-size.component'; +import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) -export class AppComponent implements OnInit, AfterViewInit { - @ViewChild(FontSizeComponent) size!: FontSizeComponent; - @ViewChild(FontSizeComponent, { static: true }) - staticSize!: FontSizeComponent; - - display = true; - - fontSize = 12; - - ngOnInit(): void { - console.log('AppComponent - ngOnInit - size', this.size); - console.log('AppComponent - ngOnInit - staticSize', this.staticSize); - } - - ngAfterViewInit(): void { - console.log('AppComponent - ngAfterViewInit - size', this.size); - console.log('AppComponent - ngAfterViewInit - staticSize', this.staticSize); - } -} +export class AppComponent {} diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 565bac6..414f47d 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,14 +1,13 @@ -import { FontSizeComponent } from './font-size/font-size.component'; import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; - +import { CustomButtonDirective } from './custom-button.directive'; @NgModule({ imports: [ BrowserModule, FormsModule ], - declarations: [ AppComponent, FontSizeComponent ], + declarations: [ AppComponent, CustomButtonDirective ], bootstrap: [ AppComponent ] }) export class AppModule { } diff --git a/src/app/custom-button.directive.ts b/src/app/custom-button.directive.ts new file mode 100644 index 0000000..51a885f --- /dev/null +++ b/src/app/custom-button.directive.ts @@ -0,0 +1,14 @@ +import { Directive, ElementRef, OnInit, Renderer2 } from '@angular/core'; + +@Directive({ + selector: '[appCustomButton]', +}) +export class CustomButtonDirective implements OnInit { + constructor(private elRef: ElementRef, private renderer: Renderer2) {} + + ngOnInit(): void { + this.renderer.setStyle(this.elRef.nativeElement, 'padding', '10px'); + this.renderer.setStyle(this.elRef.nativeElement, 'fontSize', '14pt'); + this.renderer.setStyle(this.elRef.nativeElement, 'fontWeight', 'bold'); + } +} diff --git a/src/app/font-size/font-size.component.css b/src/app/font-size/font-size.component.css deleted file mode 100644 index e69de29..0000000 diff --git a/src/app/font-size/font-size.component.html b/src/app/font-size/font-size.component.html deleted file mode 100644 index ee8ef91..0000000 --- a/src/app/font-size/font-size.component.html +++ /dev/null @@ -1,6 +0,0 @@ - - diff --git a/src/app/font-size/font-size.component.ts b/src/app/font-size/font-size.component.ts deleted file mode 100644 index 0767784..0000000 --- a/src/app/font-size/font-size.component.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Component, EventEmitter, Input, Output } from '@angular/core'; - -@Component({ - selector: 'app-font-size', - templateUrl: './font-size.component.html', - styleUrls: ['./font-size.component.css'] -}) -export class FontSizeComponent { - @Input() size!: number; - @Output() sizeChange = new EventEmitter(); - - onSetFontSize(value: number): void { - this.size += value; - this.sizeChange.emit(this.size); - } -}