Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically add a web component to a div
    primarykey
    data
    text
    <p>I started with the generating click-counter example. I made the click-counter into a library which I then imported into my main file. The click-counter component can be added manually by putting the appropriate HTML into the web page before running the program. However, I've been unable to find a way to dynamically add the click-counter web component to a div. My attempts have either ended in "Aw, snap!" errors or simply with nothing happening.</p> <p>The click-counter (xclickcounter.dart):</p> <pre class="lang-dart prettyprint-override"><code>library clickcounter; import 'package:web_ui/web_ui.dart'; class CounterComponent extends WebComponent { int count = 0; void increment() { count++; } } </code></pre> <p>The main HTML:</p> <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Sample app&lt;/title&gt; &lt;link rel="stylesheet" href="test1.css"&gt; &lt;!-- import the click-counter --&gt; &lt;link rel="components" href="xclickcounter.html"&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test1&lt;/h1&gt; &lt;p&gt;Hello world from Dart!&lt;/p&gt; &lt;div id="sample_container_id"&gt; &lt;div is="x-click-counter" id="click_counter" count="{{startingCount}}"&gt;&lt;/div&gt; &lt;/div&gt; &lt;script type="application/dart" src="test1.dart"&gt;&lt;/script&gt; &lt;script src="packages/browser/dart.js"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>main file:</p> <pre class="lang-dart prettyprint-override"><code>import 'dart:html'; import 'package:web_ui/web_ui.dart'; import 'xclickcounter.dart'; // initial value for click-counter int startingCount = 5; void main() { // no error for adding an empty button var button = new ButtonElement(); query('#sample_container_id').append(button); // doesn't work (gives "Aw, snap!" in Dartium) query('#sample_container_id').append(new CounterComponent()); // Nothing happens with this code. Nothing appears. // But I promise this same thing was giving Aw, Snap // for a very similar program final newComponentHtml = '&lt;div is="x-click-counter"&gt;&lt;/div&gt;'; query('#sample_container_id').appendHtml(newComponentHtml); } </code></pre> <p>I tried added an empty constructor to click-counter but it still crashes. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload