Browse Source

動態內容投影 ng-content Select功能

標籤(Tag) Select="標籤名" Ex:select="h2"
樣式類別(Class) select=".類別名稱" Ex:select=".className"
屬性(Attribute) select="[屬性名稱]" Ex:select="[attrName]"
master
HarveyChou 2 years ago
parent
commit
510a4a0f65
  1. 11
      src/app/app.component.html
  2. 4
      src/app/app.module.ts
  3. 24
      src/app/form-container/form-container.component.css
  4. 10
      src/app/form-container/form-container.component.html
  5. 23
      src/app/form-container/form-container.component.spec.ts
  6. 15
      src/app/form-container/form-container.component.ts

11
src/app/app.component.html

@ -1,3 +1,8 @@
<app-child>
<div>AppComponent 指定內容</div>
</app-child>
<app-form-container>
<h3>這是標題表單</h3>
<div class="sub-title">標題副表單</div>
表單內容
<div form-button>
<button type="button">儲存</button>
</div>
</app-form-container>

4
src/app/app.module.ts

@ -5,11 +5,13 @@ import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ChildComponent } from './child/child.component';
import { FormContainerComponent } from './form-container/form-container.component';
@NgModule({
declarations: [
AppComponent,
ChildComponent
ChildComponent,
FormContainerComponent
],
imports: [
BrowserModule,

24
src/app/form-container/form-container.component.css

@ -0,0 +1,24 @@
:host {
display: block;
border: solid 1px #aaa;
}
.title {
display: flex;
padding: 0 10px;
flex-direction: row;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #aaa;
}
.content {
padding: 10px;
min-height: 100px;
}
.button {
padding: 10px;
border-top: 1px solid #aaa;
text-align: center;
}

10
src/app/form-container/form-container.component.html

@ -0,0 +1,10 @@
<div class="title">
<ng-content select="h3"></ng-content>
<ng-content select=".sub-title"></ng-content>
</div>
<div class="content">
<ng-content></ng-content>
</div>
<div class="button">
<ng-content select="[form-button]"></ng-content>
</div>

23
src/app/form-container/form-container.component.spec.ts

@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormContainerComponent } from './form-container.component';
describe('FormContainerComponent', () => {
let component: FormContainerComponent;
let fixture: ComponentFixture<FormContainerComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ FormContainerComponent ]
})
.compileComponents();
fixture = TestBed.createComponent(FormContainerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

15
src/app/form-container/form-container.component.ts

@ -0,0 +1,15 @@
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-form-container',
templateUrl: './form-container.component.html',
styleUrls: ['./form-container.component.css']
})
export class FormContainerComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
Loading…
Cancel
Save