File

universal/src/directives/platform.directive.ts

Description

Provides the base functionality for platform directives to render elements only on certain platforms.

Implements

OnInit

Index

Methods

Constructor

constructor(template: TemplateRef, viewContainer: ViewContainerRef, universal: UniversalService)
Parameters:
Name Type Optional
template TemplateRef<ElementRef> No
viewContainer ViewContainerRef No
universal UniversalService No

Methods

ngOnInit
ngOnInit()

Checks whether the element should be rendered on the current platform and renders it.

Returns: void
Protected Abstract shouldRender
shouldRender()

Checks whether the element should be rendered on the current platform.

Returns: boolean
import { Directive, TemplateRef, ViewContainerRef, ElementRef, OnInit } from '@angular/core';

import { UniversalService } from '../services/universal.service';

/**
 * Provides the base functionality for platform directives to render elements only on certain platforms.
 *
 * @export
 * @abstract
 * @class PlatformDirective
 * @implements {OnInit}
 */
@Directive()
export abstract class PlatformDirective implements OnInit
{
    constructor(private template: TemplateRef<ElementRef>, private viewContainer: ViewContainerRef, protected universal: UniversalService) { }

    /**
     * Checks whether the element should be rendered on the current platform and renders it.
     */
    ngOnInit()
    {
        this.shouldRender() ? this.viewContainer.createEmbeddedView(this.template) : this.viewContainer.clear();
    }

    /**
     * Checks whether the element should be rendered on the current platform.
     *
     * @protected
     * @abstract
     * @returns {boolean}
     */
    protected abstract shouldRender(): boolean;
}

results matching ""

    No results matching ""