简体   繁体   English

Angular 2 @Input - 如何从父组件绑定子组件的 ID 属性?

[英]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 .这与HTMLElementid属性相冲突。

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.

相关问题 如何在ngSwitch的Angular 2+中将事件从子级绑定到父级组件 - How to bind event from child to parent component in Angular 2+ in ngSwitch Angular2:如何在以编程方式从子组件更改但未在父绑定中更改后重置输入属性? - Angular2: How to reset input property after it is changed from child component programatically but not changed in parent binding? 如何在 Angular 8 中将属性从父组件传递给子组件? - How to pass property from parent to child component in Angular 8? 来自父组件的角度更新子组件输入 - Angular updating child component input from parent component 如何将输入集中在父级的子级组件上? - How to focus input on a child component from parent? 如何让子组件检测来自父组件的对象 (@Input()) 在 Angular 中已更改 - How to make child component detects object (@Input()) from parent component has changed in Angular 如何将属性从一个组件绑定到角度为4的App.Component? - How to Bind Property from one component to App.Component in angular 4? Angular5-从子组件的输入的[[ngModel)]绑定更改父级的属性 - Angular5 - Change parent's property from child component's input's [(ngModel)] binding 如何从 Angular 9 中的子组件访问父组件 - How to Access Parent Component from Child Component in Angular 9 Angular 2,如何将选项从父组件传递到子组件? - Angular 2, How to pass options from parent component to child component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM