Skip to content

Instantly share code, notes, and snippets.

@arutnik
Created July 8, 2018 20:04
Show Gist options
  • Save arutnik/ff01ab0b74a07a7c01fe5534420bb534 to your computer and use it in GitHub Desktop.
Save arutnik/ff01ab0b74a07a7c01fe5534420bb534 to your computer and use it in GitHub Desktop.
SEO-SPA: DI Service Usage
//In app.browser.module.ts providers:
{
provide: ElementFocusService,
useClass: ElementFocusBrowserService
},
//In app.server.module.ts providers:
{
provide: ElementFocusService,
useClass: ElementFocusServerService
},
//Component usage:
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { ElementFocusService } from '../../services/element-focus.service';
@Component({
selector: 'app-look-at-me',
template: `
<p #myElement>I'm in focus now!</p>
`,
styleUrls: ['./look-at-me.component.scss']
})
export class LookAtMeComponent implements OnInit {
@ViewChild('myElement') myElementRef: ElementRef;
constructor(private focusElement: ElementFocusService) { }
ngOnInit() {
this.focusElement.focusElement(this.myElementRef);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment