[英]Angular 2 @Input - How to bind ID property of child component from parent component?
In my parent component, I want to create a child component with a unique ID associated with it, and I want to pass that unique ID into the child component, so the child component can put that ID on its template.在我的父组件中,我想创建一个具有与其关联的唯一 ID 的子组件,并且我想将该唯一 ID 传递给子组件,以便子组件可以将该 ID 放在其模板上。
Parent template:父模板:
<ckeditor [ckEditorInstanceID]="someUniqueID"> </ckeditor>
Here is the child component:这是子组件:
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')
});
}
}
What am I missing?我错过了什么? I can't seem to get the child component to receive the value of "someUniqueID".
我似乎无法让子组件接收“someUniqueID”的值。 it is always undefined.
它总是未定义的。
UPDATE: I was able to get the child component to receive the value "someUniqueID. Code udpated above. However, I cannot reference the @Input property by calling this.ckEditorInstanceID
because this
is undefined.更新:我能够让子组件接收值“someUniqueID。上面的代码已更新。但是,我无法通过调用
this.ckEditorInstanceID
来引用 @Input 属性,因为this
是未定义的。
How do I reference the property I brought in via @Input?如何引用我通过@Input 引入的属性?
Don't name inputs id
.不要命名输入
id
。 That's conflicting with the id
attribute of the HTMLElement
.这与
HTMLElement
的id
属性相冲突。
The trick was to use an arrow function like @David Bulte mentioned.诀窍是使用像@David Bulte 提到的箭头函数。
loadScript('//cdn.ckeditor.com/4.5.11/standard/ckeditor.js', () => {
CKEDITOR.replace(this.ckEditorInstanceID);
console.info('CKEditor loaded async')
});
For some reason the arrow function can access this.ckEditorInstanceID
, but a regular function() {} cannot access this.ckEditorInstanceID
.由于某种原因,箭头函数可以访问
this.ckEditorInstanceID
,但常规 function() {} 不能访问this.ckEditorInstanceID
。 I don't know why, maybe someone can enlighten me to the reasoning for this.我不知道为什么,也许有人可以启发我对此的推理。
In addition, I had to change my markup like this:此外,我必须像这样更改我的标记:
<ckeditor [ckEditorInstanceID]="'editor1'"> </ckeditor>
<ckeditor [ckEditorInstanceID]="'editor2'"> </ckeditor>
And set the @Input property to the name inside the [] which is ckEditorInstanceID
, and also the template source should be the property name ckEditorInstanceID
, like [id]="ckEditorInstanceID"
.并将@Input 属性设置为 [] 中的名称,即
ckEditorInstanceID
,并且模板源应该是属性名称ckEditorInstanceID
,例如[id]="ckEditorInstanceID"
。
Full working child component that receives the ID from the parent html selector:从父 html 选择器接收 ID 的完整工作子组件:
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() {}
ngAfterViewInit() {
loadScript('//cdn.ckeditor.com/4.5.11/standard/ckeditor.js', () => {
CKEDITOR.replace(this.ckEditorInstanceID);
console.info('CKEditor loaded async')
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.