javascript - Angular2 属性指令属性

标签 javascript angular ionic3

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

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

图像样式指令

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明确地说,我仍然无法访问该属性(property)。我不知道我在哪里错过了它 有什么建议吗?

最佳答案

根据上面的@jonrsharpe(在注释中),构造函数中无法访问输入。所以我将其移至 ngOnInit() 生命周期 Hook 并且它起作用了!

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

}

关于javascript - Angular2 属性指令属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47123969/

相关文章:

javascript - Bootstrap Navbar - 当前面的下拉列表折叠时,如何确保下拉列表的顶部位于视口(viewport)中?

javascript - 从 Angular 4 向 Express 发送删除请求中的数组

angular - 在 ionic 2 中将 nav.push 与侧边菜单一起使用

javascript - JavaScript 中类似 Ruby on Rails 的框架

php - 自定义 twitter 'Tweet Button' 回调不适用于第一次点击但第二次有效

javascript - ionic v3 : Group list by date/day

html - 不能在 .scroll-content 上使用 css,因为它会破坏 ion-infinite-scroll

ionic-framework - Ionic Native + IOS + OneSignal

javascript - 每当节点服务器上的数据发生变化时写入整个 SQL 表(很奇怪,请耐心等待)

angular - ngx-translate 特殊字符问题