从标题开始,我想了解如何使用目录获取图像src值
指令:
[w.xpath('string()').extract_first().strip() for w in response.xpath('//ul[@class="attribute-list"]/li/dl/dd')]
HTML:
@Directive({
selector: '[appLazyImage]',
})
export class LazyImageDirective implements OnInit {
@HostBinding('src') public src: string;
@Input() private appLazyImage: string;
constructor() {}
public ngOnInit() {
//this.src is undefined
console.log(this.appLazyImage, this.src);
}
}
答案 0 :(得分:1)
使用ElementRef服务获取Src属性
import { Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[appSrc]'
})
export class SrcDirective {
constructor(private el: ElementRef) {
console.log(this.el.nativeElement.src);
}
}
答案 1 :(得分:1)
使用nativeElement
获取属性值。
constructor(elm: ElementRef) {
let src = elm.nativeElement.getAttribute('src');
}