我想创建一个自定义属性指令并将其绑定到属性.我计划检索此属性并稍后获取值.
我创建了一个指令:
@Directive({
selector: '[data-url]'
})
export class DocumentURL{
constructor(private el: ElementRef, private renderer: Renderer) { }
}
Run Code Online (Sandbox Code Playgroud)
这是使用该指令的组件:
@Component({
templateUrl: 'some.component.html',
directives: [DocumentURL]
})
Run Code Online (Sandbox Code Playgroud)
这是我的some.component.html:
<div class="col-xs-6" [data-url]='docUrl' (mouseleave)='onMouseLeave($event)'>
Run Code Online (Sandbox Code Playgroud)
但是,它抛出一个错误说:无法绑定到'data-url',因为它不是已知的本机属性
有人可以帮忙吗?谢谢.
您必须@Input在指令中添加属性.
@Directive({
selector: '[data-url]'
})
export class DocumentURL{
@Input('data-url')
dataUrl:string;
constructor(private el: ElementRef, private renderer: Renderer) { }
}
Run Code Online (Sandbox Code Playgroud)
见plunkr.
| 归档时间: |
|
| 查看次数: |
4474 次 |
| 最近记录: |