Skip to content

Instantly share code, notes, and snippets.

@m-abs
Created May 7, 2018 16:04

Revisions

  1. m-abs created this gist May 7, 2018.
    26 changes: 26 additions & 0 deletions update-label.directive.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import { Directive, ElementRef, Input } from '@angular/core';
    import { Label } from 'tns-core-modules/ui/label';

    /**
    * Make sure the layout is updated then the Label's text is changed.
    */
    @Directive({
    // tslint:disable-next-line:directive-selector
    selector: 'Label[text]',
    })
    export class UpdateLabelDirective {
    @Input('text')
    public set _text(text: string) {
    const nativeView = this.nativeView;
    if (nativeView && nativeView instanceof Label) {
    nativeView.text = text;
    nativeView.requestLayout();
    }
    }

    private get nativeView(): Label {
    return this.el.nativeElement;
    }

    constructor(private readonly el: ElementRef) {}
    }