question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create component dynamically

See original GitHub issue

Hi, what’s the proper way to create component dynamically

I’ve tried:

const x = new $ui.MyWidget(parent);
parent.addChild(x);

but x has no children and properties and their bindings, if but it seems it’s not suggested to call $c $s manually.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
whoozlecommented, Nov 30, 2019

Hi Thanks for reaching us out 😃

There’s a few ways of doing this, and instantiation is the most complex of them. 😃

The easiest way of creating components dynamically is Loader: https://pureqml.com/docs/core/Loader.html

If you really want to create components dynamically, you can use the following snippet from Loader

		var item = new <COMPONENT>(PARENT);
		item.__init(); //this will call all creation phases ($c and $s)
		this._context.scheduleComplete() //run onCompleted, you can call it just once after all components have been created
		this._updateVisibilityForChild(this.item, this.recursiveVisible); //this is needed for visible/recursiveVisible to work
		this._tryFocus(); //this will set focus if needed

__init is a shortcut for single component creation. If you want to create more than one component, you have to do it in more phases

for all components you have to call

  1. ctor (with proper parent)
  2. $c (this will add ids)
  3. $s (this will install all handlers and schedule async handlers)
  4. this._updateVisibilityForChild(this.item, this.recursiveVisible)
  5. this._tryFocus()

after that you have to call this._context.scheduleComplete()

addChild is not strictly needed, only if you want the object appear among .children

0reactions
whoozlecommented, Feb 25, 2020

starting with 07098447450bbb49ed1bc8c3880ef49fce699bb4, it’s possible to just

var foo = new Foo(parent)
$core.core.createObject(foo)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic component loader - Angular
To add the component to the template, you call createComponent() on ViewContainerRef . The createComponent() method returns a reference to the loaded component....
Read more >
Dynamically Creating Components with Angular - Netanel Basal
In this article, we will learn how to create components in Angular dynamically. First, we need a component. For the simplicity, we are...
Read more >
Creating Components Dynamically with Component Factory in ...
If we go by the Angular definition, a factory component Angular is a base class for a factory that can create a component...
Read more >
Dynamically Creating Components in Angular
Needed a way to get a container to inject the component into. Needed a way to generate a component (just calling new Gauge()...
Read more >
Dynamically Add Components in Angular - Dave Bush - Medium
Before you create a component dynamically, you need to retrieve the component factory for it. And in order to get the factory, you'll...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found