Przeglądaj źródła

passing template ref

Kroenen62 5 lat temu
rodzic
commit
42991f1d5d

BIN
angular-tooltip-html.zip Wyświetl plik


+ 2
- 2
angular.json Wyświetl plik

@@ -7,7 +7,7 @@
7 7
       "root": "",
8 8
       "sourceRoot": "src",
9 9
       "projectType": "application",
10
-      "prefix": "app",
10
+      "prefix": "c3",
11 11
       "schematics": {
12 12
         "@schematics/angular:component": {
13 13
           "style": "sass"
@@ -137,4 +137,4 @@
137 137
     }
138 138
   },
139 139
   "defaultProject": "angular-tooltip-html"
140
-}
140
+}

+ 4
- 20
src/app/app.component.html Wyświetl plik

@@ -1,20 +1,4 @@
1
-<!--The content below is only a placeholder and can be replaced.-->
2
-<div style="text-align:center">
3
-  <h1>
4
-    Welcome to {{ title }}!
5
-  </h1>
6
-  <img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
7
-</div>
8
-<h2>Here are some links to help you start: </h2>
9
-<ul>
10
-  <li>
11
-    <h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
12
-  </li>
13
-  <li>
14
-    <h2><a target="_blank" rel="noopener" href="https://angular.io/cli">CLI Documentation</a></h2>
15
-  </li>
16
-  <li>
17
-    <h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
18
-  </li>
19
-</ul>
20
-
1
+<app-componentWithContentOrNot>
2
+	<button [c3Tooltip]="content"></button>
3
+	<c3-tooltip #content="c3Tooltip" ></c3-tooltip>
4
+</app-componentWithContentOrNot>

+ 16
- 8
src/app/app.module.ts Wyświetl plik

@@ -2,15 +2,23 @@ import { BrowserModule } from '@angular/platform-browser';
2 2
 import { NgModule } from '@angular/core';
3 3
 
4 4
 import { AppComponent } from './app.component';
5
+import { ComponentWithContentOrNotComponent } from './componentWithContentOrNot/componentWithContentOrNot.component';
6
+import { HasContentComponent } from './hasContent/hasContent.component';
7
+import { C3TooltipModule } from './c3-tooltip/c3-tooltip.module';
5 8
 
6 9
 @NgModule({
7
-  declarations: [
8
-    AppComponent
9
-  ],
10
-  imports: [
11
-    BrowserModule
12
-  ],
13
-  providers: [],
14
-  bootstrap: [AppComponent]
10
+   declarations: [
11
+      AppComponent,
12
+      ComponentWithContentOrNotComponent,
13
+      HasContentComponent
14
+   ],
15
+   imports: [
16
+			BrowserModule,
17
+			C3TooltipModule
18
+   ],
19
+   providers: [],
20
+   bootstrap: [
21
+      AppComponent
22
+   ]
15 23
 })
16 24
 export class AppModule { }

+ 0
- 0
src/app/c3-tooltip/c3-tooltip.component.css Wyświetl plik


+ 5
- 0
src/app/c3-tooltip/c3-tooltip.component.html Wyświetl plik

@@ -0,0 +1,5 @@
1
+<ng-template>	
2
+	<p>
3
+		c3-tooltip works!
4
+	</p>
5
+</ng-template>

+ 17
- 0
src/app/c3-tooltip/c3-tooltip.component.ts Wyświetl plik

@@ -0,0 +1,17 @@
1
+import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
2
+
3
+@Component({
4
+	selector: 'c3-tooltip',
5
+	templateUrl: './c3-tooltip.component.html',
6
+	styleUrls: ['./c3-tooltip.component.css'],
7
+	exportAs: 'c3Tooltip'
8
+})
9
+export class C3Tooltip implements OnInit {
10
+	@ViewChild(TemplateRef, { read: false }) template: TemplateRef<any>;
11
+
12
+	constructor() { }
13
+
14
+	ngOnInit() {
15
+	}
16
+
17
+}

+ 19
- 0
src/app/c3-tooltip/c3-tooltip.module.ts Wyświetl plik

@@ -0,0 +1,19 @@
1
+import { NgModule } from '@angular/core';
2
+import { CommonModule } from '@angular/common';
3
+import { C3Tooltip } from './c3-tooltip.component';
4
+import { C3TooltipTrigger } from './c3-tooltipTrigger.directive';
5
+
6
+@NgModule({
7
+   imports: [
8
+		CommonModule
9
+   ],
10
+   declarations: [
11
+		C3Tooltip,
12
+		C3TooltipTrigger
13
+	 ],
14
+	 exports: [
15
+		C3Tooltip,
16
+		C3TooltipTrigger
17
+	 ]
18
+})
19
+export class C3TooltipModule { }

+ 28
- 0
src/app/c3-tooltip/c3-tooltipTrigger.directive.ts Wyświetl plik

@@ -0,0 +1,28 @@
1
+import { Directive, forwardRef, Input, AfterViewInit, ElementRef, ViewContainerRef } from '@angular/core';
2
+import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
3
+import { C3Tooltip } from './c3-tooltip.component';
4
+
5
+export const MAT_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
6
+	provide: NG_VALUE_ACCESSOR,
7
+	useExisting: forwardRef(() => C3TooltipTrigger),
8
+	multi: true
9
+};
10
+
11
+@Directive({
12
+	selector: 'button[c3Tooltip]',
13
+	exportAs: 'c3TooltipTrigger',
14
+	providers: [MAT_AUTOCOMPLETE_VALUE_ACCESSOR]
15
+})
16
+export class C3TooltipTrigger implements AfterViewInit {
17
+	@Input('c3Tooltip') tooltip: C3Tooltip;
18
+
19
+	constructor(
20
+		private _element: ElementRef<HTMLInputElement>,
21
+		// private _overlay: Overlay,
22
+		private _viewContainerRef: ViewContainerRef) {
23
+	}
24
+
25
+	ngAfterViewInit() {
26
+		console.log(this.tooltip.template)
27
+	}
28
+}

+ 0
- 0
src/app/componentWithContentOrNot/componentWithContentOrNot.component.css Wyświetl plik


+ 11
- 0
src/app/componentWithContentOrNot/componentWithContentOrNot.component.html Wyświetl plik

@@ -0,0 +1,11 @@
1
+<div *ngIf="hasChild">J'ai un enfant !</div>
2
+<ng-container *ngIf="hasChild; else elseTemplate">
3
+	J'ai un enfant !
4
+</ng-container>
5
+<ng-template #elseTemplate>
6
+	Je n'ai pas d'enfant !
7
+</ng-template>
8
+
9
+<app-content [(hasChild)]="hasChild" (hasChildChange)="hasChildChange($event)">
10
+	<ng-content></ng-content>
11
+</app-content>

+ 28
- 0
src/app/componentWithContentOrNot/componentWithContentOrNot.component.spec.ts Wyświetl plik

@@ -0,0 +1,28 @@
1
+/* tslint:disable:no-unused-variable */
2
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3
+import { By } from '@angular/platform-browser';
4
+import { DebugElement } from '@angular/core';
5
+
6
+import { ComponentWithContentOrNotComponent } from './componentWithContentOrNot.component';
7
+
8
+describe('ComponentWithContentOrNotComponent', () => {
9
+  let component: ComponentWithContentOrNotComponent;
10
+  let fixture: ComponentFixture<ComponentWithContentOrNotComponent>;
11
+
12
+  beforeEach(async(() => {
13
+    TestBed.configureTestingModule({
14
+      declarations: [ ComponentWithContentOrNotComponent ]
15
+    })
16
+    .compileComponents();
17
+  }));
18
+
19
+  beforeEach(() => {
20
+    fixture = TestBed.createComponent(ComponentWithContentOrNotComponent);
21
+    component = fixture.componentInstance;
22
+    fixture.detectChanges();
23
+  });
24
+
25
+  it('should create', () => {
26
+    expect(component).toBeTruthy();
27
+  });
28
+});

+ 21
- 0
src/app/componentWithContentOrNot/componentWithContentOrNot.component.ts Wyświetl plik

@@ -0,0 +1,21 @@
1
+import { Component, OnInit, ElementRef, ViewChild, ChangeDetectorRef } from '@angular/core';
2
+
3
+@Component({
4
+  selector: 'app-componentWithContentOrNot',
5
+  templateUrl: './componentWithContentOrNot.component.html',
6
+  styleUrls: ['./componentWithContentOrNot.component.css']
7
+})
8
+export class ComponentWithContentOrNotComponent implements OnInit {
9
+	// @ViewChild('content') content: ElementRef;
10
+
11
+  constructor(
12
+		private changeDetector: ChangeDetectorRef
13
+	) {}
14
+
15
+  ngOnInit() {
16
+	}
17
+	
18
+	hasChildChange(event) {
19
+		this.changeDetector.detectChanges()
20
+	}
21
+}

+ 0
- 0
src/app/hasContent/hasContent.component.css Wyświetl plik


+ 3
- 0
src/app/hasContent/hasContent.component.html Wyświetl plik

@@ -0,0 +1,3 @@
1
+<div #content>
2
+	<ng-content ></ng-content>
3
+</div>

+ 28
- 0
src/app/hasContent/hasContent.component.spec.ts Wyświetl plik

@@ -0,0 +1,28 @@
1
+/* tslint:disable:no-unused-variable */
2
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3
+import { By } from '@angular/platform-browser';
4
+import { DebugElement } from '@angular/core';
5
+
6
+import { HasContentComponent } from './hasContent.component';
7
+
8
+describe('HasContentComponent', () => {
9
+  let component: HasContentComponent;
10
+  let fixture: ComponentFixture<HasContentComponent>;
11
+
12
+  beforeEach(async(() => {
13
+    TestBed.configureTestingModule({
14
+      declarations: [ HasContentComponent ]
15
+    })
16
+    .compileComponents();
17
+  }));
18
+
19
+  beforeEach(() => {
20
+    fixture = TestBed.createComponent(HasContentComponent);
21
+    component = fixture.componentInstance;
22
+    fixture.detectChanges();
23
+  });
24
+
25
+  it('should create', () => {
26
+    expect(component).toBeTruthy();
27
+  });
28
+});

+ 20
- 0
src/app/hasContent/hasContent.component.ts Wyświetl plik

@@ -0,0 +1,20 @@
1
+import { Component, OnInit, ViewChild, ElementRef, AfterViewInit, Output, EventEmitter, Input } from '@angular/core';
2
+import { Content } from '@angular/compiler/src/render3/r3_ast';
3
+
4
+@Component({
5
+  selector: 'app-content',
6
+  templateUrl: './hasContent.component.html',
7
+  styleUrls: ['./hasContent.component.css']
8
+})
9
+export class HasContentComponent implements AfterViewInit {
10
+	@ViewChild('content') content: ElementRef;
11
+	@Input() hasChild: Boolean = false;
12
+	@Output() hasChildChange : EventEmitter<Boolean> = new EventEmitter<Boolean>()
13
+
14
+  constructor() { }
15
+
16
+	ngAfterViewInit(){
17
+		this.hasChildChange.emit(!!this.content.nativeElement.childNodes.length);
18
+	}
19
+
20
+}