简体   繁体   English

如何解析Angular 2中动态添加的组件的值?

[英]How to resolve value of dynamically added component in Angular 2?

I have modal component consists of device information in which i am dynamically adding user <user.component.ts> given below. 我的模态组件由设备信息组成,我在其中动态添加下面给出的用户<user.component.ts> It's getting added & rendered successfully but somehow i am unable to fetch the details of selected user in modal <modal.component.ts> . 它已成功添加和呈现,但是以某种方式我无法在modal <modal.component.ts>获取所选用户的详细信息。

modal.component.ts modal.component.ts

@Component({
  selector: 'modal',
  templateUrl: './modal.component.html'
})
export class ModalComponent {
    constructor(injector: Injector, 
            componentFactoryResolver: ComponentFactoryResolver) {}

    @ViewChild('userContainer', { read: ViewContainerRef }) userContainer: ViewContainerRef;
    cmp: ComponentRef<UserListComponent>;

    addComponent() {
        const factory = this.componentFactoryResolver.resolveComponentFactory(UserListComponent);
        this.cmp = this.userContainer.createComponent(factory);
    }
}

user.component.ts user.component.ts

@Component({
  selector: 'user',
  templateUrl: `<select [(ngModel)]="user">
           <option *ngFor="let user of users" [ngValue]="user">{{user.name}}</option>
         </select>`
})
export class UserComponent {...}

Use an EventEmitter or Observable that emits values on change 使用EventEmitterObservable在更改时发出值

@Component({
  selector: 'user',
  templateUrl: `<select [(ngModel)]="user">
           <option *ngFor="let user of users" [ngValue]="user">{{user.name}}</option>
         </select>`
})
export class UserComponent {
    @Output() userChange:EventEmitter = new EventEmitter();

    private _user:any;
    get user () {
      return this._user;
    }

    set user(val) {
      this._user = val;
      this.userChange.emit(val);
    }
}

and subscribe to the changes like: 并订阅以下更改:

addComponent() {
    const factory = this.componentFactoryResolver.resolveComponentFactory(UserListComponent);
    this.cmp = this.userContainer.createComponent(factory);
    this.cmp.instance.userChange.subscribe(val => console.log(val));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更新 Angular 中动态添加的子组件中的值 9 - How to update a value in the dynamically added child component in Angular 9 如何将Angular的模拟封装属性应用于动态添加到Angular组件的HTML元素? - How to apply Angular's emulated encapsulation attributes to HTML elements added dynamically to Angular component? 在 Angular 中获取动态添加的文本框值 - Getting dynamically added textbox value in Angular angular-2中的服务如何在每个组件中动态获得价值? - how service in angular-2 gets value dynamically in every component? 在 Angular 5 中,如何从父组件访问动态添加的子组件? - In Angular 5, how to get access to dynamically added child components from parent component? 从 Angular 6 CLI 中动态添加的组件引用服务 - Reference to service from dynamically added component in Angular 6 CLI 没有从组件中以角度动态添加的值 - not getting values which are added dynamically from the component in angular 无法绑定到Angular组件的Bootstrap模态中动态添加的输入 - Can't bind to dynamically added input in Bootstrap modal of Angular component 如何以角度动态删除组件 - How to remove a component dynamically in angular 如何在 Angular 中动态导入组件? - How dynamically import component in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM