【问题标题】:Angular2 attribute directive propertyAngular2 属性指令属性
【发布时间】:2017-11-05 16:35:41
【问题描述】:

我正在尝试制作一个 angular2(4) 指令。我用它来设置 div 的样式,并添加背景图像。背景图像的源由组件使用该指令传递。

我无法访问输入。它不断返回undefined 这就是我所做的

image-style 指令

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 使用指令

<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>

这是我第一次尝试这样的事情。我明确关注了documentation,但我仍然无法访问该物业。我不确定我错过了哪里 有什么建议吗?

【问题讨论】:

  • 它在构造函数中必然是未定义的,直到生命周期的后期才能访问输入。
  • 您需要将指令应用到 div。
  • @jonrsharpe 成功了!谢谢。我在ngOnInit() 生命周期挂钩中创建了this._setProperties()

标签: javascript angular ionic3


【解决方案1】:

根据上面的@jonrsharpe(在 cmets 中)输入在构造函数中不可访问。所以我把它移到 ngOnInit() 生命周期钩子上,它起作用了!

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+"')";
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 2017-12-30
    • 2017-06-18
    • 2015-10-27
    • 1970-01-01
    • 2013-10-26
    • 2016-05-23
    • 2018-02-11
    相关资源
    最近更新 更多