简体   繁体   中英

Angular2 attribute directive property

I am trying to make an angular2(4) directive. i use it to style the div, and add a background image. the source of the background image is passed by the component using that directive.

I am not able to get access to the input. it keeps returning undefined Heres what i did

image-style directive

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({
    selector: '[image-style]' // Attribute selector
})
export class ImageStyleDirective {
    @Input("image-style") imageSrc: string;

    constructor(public el: ElementRef) {
        console.log('Hello ImageStyleDirective Directive');
        console.log(this.imageSrc); //THIS RETURNS UNDEFINED EVERY TIME

        this._setProperties();
    }

    _setProperties(){
        console.log(this.imageSrc);

        this.el.nativeElement.style.width = "70px";
        this.el.nativeElement.style.height = "70px";
        this.el.nativeElement.style.borderRadius = "50%";

        this.el.nativeElement.style.backgroundSize = "contain";
        this.el.nativeElement.style.backgroundPosition = "center center";
        this.el.nativeElement.style.backgroundRepeat = "no-repeat";
        this.el.nativeElement.style.backgroundColor = "#2d2439";

        this.el.nativeElement.style.backgroundImage = "url('"+this.imageSrc+"')";
    }

comonent.html using the directive

<div class="item-block">
    <ion-grid>
        <ion-row align-items-center>
            <ion-col class="image">
                <div [image-style]="brand.image"></div>
            </ion-col>
            <ion-col col-9>
                <div class="name">{{brand.name}}</div>
                <div class="category">{{brand.category}}</div>
                <div class="location">{{brand.location}}</div>
            </ion-col>
        </ion-row>
    </ion-grid>
</div>

This is my first time attempting something like this. i followed the documentation explicitly and i still don't have access to the property. I am not sure where i missed it Any suggestions?

According to @jonrsharpe above(in the comments) The input is not accessible in the constructor. So i moved it to the ngOnInit() lifecycle hook and it worked!

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({
    selector: '[image-style]' // Attribute selector
})
export class ImageStyleDirective {
    @Input("image-style") imageSrc;

    constructor(public el: ElementRef) {
        console.log('Hello ImageStyleDirective Directive');
    }

    ngOnInit(){
        console.log(this.imageSrc);
        this._setProperties();
    }

    _setProperties(){
        console.log(this.imageSrc);

        this.el.nativeElement.style.width = "70px";
        this.el.nativeElement.style.height = "70px";
        this.el.nativeElement.style.borderRadius = "50%";
        // max-width: 40%;
        // this.el.nativeElement.style.margin = "auto";
        this.el.nativeElement.style.backgroundSize = "contain";
        this.el.nativeElement.style.backgroundPosition = "center center";
        this.el.nativeElement.style.backgroundRepeat = "no-repeat";
        this.el.nativeElement.style.backgroundColor = "#2d2439";

        this.el.nativeElement.style.backgroundImage = "url('"+this.imageSrc+"')";
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM