Javascript 如何更新动态创建的组件的模型?
我已经动态创建了组件 使用此代码:Javascript 如何更新动态创建的组件的模型?,javascript,angular,Javascript,Angular,我已经动态创建了组件 使用此代码: const inputProviders = Object.keys(data.inputs).map( (inputName) => { return { provide: inputName, useValue: data.inputs[inputName] }; }); const resolvedInputs = ReflectiveInjector.resolve(inpu
const inputProviders = Object.keys(data.inputs).map(
(inputName) => {
return { provide: inputName, useValue: data.inputs[inputName] };
});
const resolvedInputs = ReflectiveInjector.resolve(inputProviders);
// We create an injector out of the data we want to pass down and this components injector
const injector = ReflectiveInjector.fromResolvedProviders(resolvedInputs, this.dynamicComponentContainer.parentInjector);
// We create a factory out of the component we want to create
const factory = this.resolver.resolveComponentFactory(data.component);
// We create the component using the factory and the injector
const component = factory.create(injector);
// We insert the component into the dom container
this.dynamicComponentContainer.insert(component.hostView);
// Destroy the previously created component
if (this.currentComponent) {
this.currentComponent.destroy();
}
this.currentComponent = component;
}
constructor(private resolver: ComponentFactoryResolver) { }
组件名称是包含一个数组的组件
信息
但现在每当服务器发出通知时,我都会更新
我想在消息数组中推送消息
像这样`
export class NotificationHandler {
constructor( @Inject(forwardRef(() => ChatComponent)) private _chatComponent: ChatComponent) { }
receiveMessage(chatMessage) {
console.log('sending message', this._chatComponent.messages);
this._chatComponent.messages.push(chatMessage);
}
}
但这似乎是该组件的不同实例,因为其中未定义消息数组,而在UI上,我可以看到消息是可见的您可能需要提供UI代码。我怀疑您的UI只是检查消息引用,而不是任何推送更改。只有ChatComponent的子组件可以向ChatComponent注入引用并获得正确的实例。NotificationHandler看起来像一个服务。您可以将服务注入ChatComponent,ChatComponent将使用rxjs提取消息。例如,您可能需要提供UI代码。我怀疑您的UI只是检查消息引用,而不是任何推送更改。只有ChatComponent的子组件可以向ChatComponent注入引用并获得正确的实例。NotificationHandler看起来像一个服务。您可以将服务注入ChatComponent,ChatComponent将使用rxjs提取消息