【发布时间】:2017-02-15 12:15:31
【问题描述】:
在我的父组件中,我想创建一个具有与其关联的唯一 ID 的子组件,并且我想将该唯一 ID 传递给子组件,以便子组件可以将该 ID 放在其模板中。
父模板:
<ckeditor [ckEditorInstanceID]="someUniqueID"> </ckeditor>
这是子组件:
import { Component, Input } from '@angular/core'
var loadScript = require('scriptjs');
declare var CKEDITOR;
@Component({
selector: 'ckeditor',
template: `<div [id]="ckEditorInstanceID">This will be my editor</div>`
})
export class CKEditor {
@Input() ckEditorInstanceID: string;
constructor() {
console.log(this.ckEditorInstanceID)
}
ngOnInit() {
}
ngAfterViewInit() {
loadScript('//cdn.ckeditor.com/4.5.11/standard/ckeditor.js', function() {
CKEDITOR.replace(this.ckEditorInstanceID);
console.info('CKEditor loaded async')
});
}
}
我错过了什么?我似乎无法让子组件接收“someUniqueID”的值。它总是未定义的。
更新:我能够让子组件接收值“someUniqueID。上面更新的代码。但是,我无法通过调用this.ckEditorInstanceID 来引用@Input 属性,因为this 是未定义。
如何引用我通过@Input 引入的属性?
【问题讨论】:
-
this.ckEditorInstanceId 不起作用,你必须使用箭头函数
-
我该怎么做?
-
iso function() {} 你写的 () => {}
-
箭头函数有块作用域
-
如果您可以发布代码示例,我们将不胜感激。我的大脑不知道该做什么:-/
标签: javascript angular input typescript output