<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Trotyl Yu on Medium]]></title>
        <description><![CDATA[Stories by Trotyl Yu on Medium]]></description>
        <link>https://medium.com/@trotyl?source=rss-ee2fdb963e1c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*W5P71V4HDn_dSdLTgtxxWA.jpeg</url>
            <title>Stories by Trotyl Yu on Medium</title>
            <link>https://medium.com/@trotyl?source=rss-ee2fdb963e1c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 19:29:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@trotyl/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introducing NG-VDOM: A new way to write Angular application]]></title>
            <link>https://medium.com/angular-in-depth/introducing-to-ng-vdom-a-new-way-to-write-angular-application-60a3be805e59?source=rss-ee2fdb963e1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/60a3be805e59</guid>
            <category><![CDATA[virtual-dom]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jsx]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Trotyl Yu]]></dc:creator>
            <pubDate>Mon, 28 Jan 2019 11:00:23 GMT</pubDate>
            <atom:updated>2020-04-29T21:27:23.794Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ElK5KpMR-RPM-O0PpYLKlw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/0*pz4GtlYKj0QArsLt.png" /></figure><blockquote><a href="https://medium.com/angular-in-depth"><strong><em>AngularInDepth</em></strong></a><strong><em> is moving away from Medium. More recent articles are hosted on the new platform </em></strong><a href="https://indepth.dev/angular/"><strong><em>inDepth.dev</em></strong></a><strong><em>. Thanks for being part of indepth movement!</em></strong></blockquote><p>Front end development has evolved dramatically in the last decade. With AngularJS released back in 2009, <strong>declarative representation of UI </strong>has become a standard approach for front-end web development. Angular makes declarative programming first class citizen by utilizing templates in components. Using static templates to define logic has a number of benefits and we’ll review them in this article.</p><p>However, there’s an alternative approach to describing UI representation through an expression-based view definition known as Virtual DOM. This approach uses JavaScript to define the view and is the approach implemented by React. In this article I want to describe it and introduce a library I wrote to use this approach in Angular.</p><h3>The benefits of a templating approach</h3><p><a href="https://angular.io/guide/glossary#component">Component</a> is a fundamental concept in Angular, responsible for defining UI representation and logic. Every component in Angular has a template. Components are modeled after the <a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel">MVVM</a> architecture, which is explained <a href="https://angular.io/guide/template-syntax#template-syntax">on Angular docs</a>:</p><blockquote>You may be familiar with the component/template duality from your experience with model-view-controller (MVC) or model-view-viewmodel (MVVM). In Angular, the component plays the part of the controller/viewmodel, and the template represents the view.</blockquote><p>A component and its template are connected by the public shape of Component, then they together could instantiate the View. A simple Angular component could look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a8b73778622c34c9807987297134c5f/href">https://medium.com/media/6a8b73778622c34c9807987297134c5f/href</a></iframe><p>With its template to be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/459bc473930e47336d7092072d8c2055/href">https://medium.com/media/459bc473930e47336d7092072d8c2055/href</a></iframe><p>Defining view through templates has a lot of advantages.</p><h4>Separation of Concern</h4><p>One can fully focus on UI when writing templates, without worrying about data sources, their dependency graph and presentation logic. For example, <a href="https://github.com/angular/material2/blob/bc8fc75bf8af82378077d7c2277e31a1dcd6aac9/src/lib/datepicker/calendar-header.html#L1-L25">a template in Angular Material</a> datepicker looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e5f6fe03bdfda38ca5e80423aab4a24/href">https://medium.com/media/0e5f6fe03bdfda38ca5e80423aab4a24/href</a></iframe><p>The template only defines the structure and contents, omitting the implementation details. The real origin of <strong>periodButtonText</strong> сan be component input, internal state or computed result, which makes no difference to the template.</p><p>By using a top-down development process, one can make the templates first, driving the shape of the component, ensure an effective design of the data layer.</p><h4>Simplicity</h4><p>With an abstraction layer, the template could help simplify the control flow, with a simple attribute on the tag:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8fb94b7c15ea5371f8520ffe56522ef/href">https://medium.com/media/c8fb94b7c15ea5371f8520ffe56522ef/href</a></iframe><p>On the contrary, implementing control flow inside expressions is troublesome and inevitably requires extra conditional operators or callbacks. Even when using <a href="https://www.npmjs.com/package/babel-plugin-jsx-control-statements">JSX Control Statements</a> to remain declarative, an extra nesting level would still break the VCS history.</p><p>Also, with the idea of Pipes (possibly known as Filters in other systems), one can elegantly handle complex data structures, such as <a href="https://rxjs.dev/api/index/class/Observable">Observables</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a88033b3786fa9aa6cd4dcd4e83169d2/href">https://medium.com/media/a88033b3786fa9aa6cd4dcd4e83169d2/href</a></iframe><p>Rather than repeating <strong>subscribe</strong> and <strong>unsubscribe</strong> in each consumption.</p><h4>Consistent `this`</h4><p>As a consumer of a component’s state, a template can make direct calls to the instance, rather than asking a component to pass them out. Without passing the method reference, all of the following codes have the same result:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28c46fa72fedeae93363d92ea8b6e160/href">https://medium.com/media/28c46fa72fedeae93363d92ea8b6e160/href</a></iframe><h4>Performance Optimization</h4><p>With concrete knowledge of which parts of the template are dynamic, the template:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96c5638ee3f1d8d0296b4fe7469f7951/href">https://medium.com/media/96c5638ee3f1d8d0296b4fe7469f7951/href</a></iframe><p>Will be compiled to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f0c3dc55f5f05603a9a8152c4f4b4e5/href">https://medium.com/media/6f0c3dc55f5f05603a9a8152c4f4b4e5/href</a></iframe><p>So that only the bindings need to be handled in the update pass.</p><p>In an expression-based dynamic view description, all the parts need to be checked in each component during the change detection cycle, or require an extra setup during bootstrap to determine the dynamic parts. Both ways would introduce extra runtime costs.</p><p>However, there are some restrictions for the approach with a template when having a complex control flow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e331f4dfda9771fe0baf5b94c821b301/href">https://medium.com/media/e331f4dfda9771fe0baf5b94c821b301/href</a></iframe><p>In the example above, making a dynamic heading level via template will result in bloated code.</p><h3>A New Possibility</h3><p>Given there are some conditions that can’t be handled elegantly with a templating approach. Can we make it possible to write an expression-based view definition in Angular when needed? The answer is Yes. And this is where our library comes in handy. When we’re using the library, we just need to extend from <strong>Renderable</strong> and implement the <strong>render</strong> method similar to React. Here’s an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc7165d7557eb6f894a0d247ddd57e85/href">https://medium.com/media/dc7165d7557eb6f894a0d247ddd57e85/href</a></iframe><p>This implementation will result into the <strong>“Hello, Angular!”</strong> string rendered in the browser. (<a href="https://codesandbox.io/s/wqomov90k">live example</a>)</p><blockquote>Both <a href="https://github.com/angular/angular/pull/20556">Angular</a> and <a href="https://github.com/angular/angular-cli/pull/11407">Angular CLI</a> already support .tsx file. Moreover, one can actually change “tsconfig.json” with a different “jsxFactory” option, rather than writing “import * as React” in the component.</blockquote><p>Besides <a href="http://www.typescriptlang.org/docs/handbook/jsx.html#intrinsic-elements">intrinsic elements</a>, both Class Component and Function Component can also be rendered (not fully compatible with React API yet). But most importantly, one can render another canonical Angular Component (<a href="https://codesandbox.io/s/qk0x5l9rvw">live example</a>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e966c48f867d67bc77ce3d6a461258a/href">https://medium.com/media/5e966c48f867d67bc77ce3d6a461258a/href</a></iframe><p><strong>HelloComponent</strong> here is a plain Angular Component with nothing special, but currently we need to register it in <strong>entryComponents</strong>. In the current implementation of Angular a component is resolved through the <a href="https://angular.io/api/core/ComponentFactory">ComponentFactory</a>. If not found, it would be treated as a Function Component which would cause an error.</p><blockquote>The concept of <strong>entryComponent</strong> will be significantly improved after Ivy.</blockquote><p>The mapping here can be illustrated as:</p><ul><li>Prop: foo -&gt; \@Input(‘foo’)</li><li>Prop: onBar -&gt; \@Output(‘bar’)</li><li>Prop: children -&gt; Content Projection</li></ul><p>Please be aware the template names of Input/Output will be used rather than property names, but they’re likely to be equivalent in most components.</p><blockquote><strong><em>A fully-featured TODO MVC example that uses the library can be found on </em></strong><a href="https://github.com/trotyl/ng-renderable-todomvc"><strong><em>GitHub</em></strong></a><strong><em> with </em></strong><a href="https://trotyl.github.io/ng-renderable-todomvc/"><strong><em>online preview available</em></strong></a><strong><em>.</em></strong></blockquote><h3>From Template to ViewModel</h3><p>To understand how Virtual DOMs can be handled in Angular, we should first understand what Virtual DOM is. Briefly, Virtual DOM is just a superfluous name for ViewModel that holds values for dynamic parts of a template. Given a fully static template, its ViewModel is empty:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9df4b1fe0227941d434cb989f3631083/href">https://medium.com/media/9df4b1fe0227941d434cb989f3631083/href</a></iframe><p>When we define all the properties of an element as dynamic and extract them to bindings, the ViewModel becomes the container of properties and their values:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68dcc270017459c1010fc54dd0808cc6/href">https://medium.com/media/68dcc270017459c1010fc54dd0808cc6/href</a></iframe><p>Then the binding data-source can be made “the container of properties” itself, rather than individual traits:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd67234756281e00fe3595b6137a10d1/href">https://medium.com/media/dd67234756281e00fe3595b6137a10d1/href</a></iframe><p>Finally, once the tag name is also a dynamic binding, the template no longer has any static parts and actually becomes redundant:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a1f7bc78263a9297ce356c6236c1d46/href">https://medium.com/media/7a1f7bc78263a9297ce356c6236c1d46/href</a></iframe><p><strong>When the ViewModel carries all the information of the View, it becomes a definition of the View. </strong>No matter what it’s being called, it’s still a ViewModel and can be treated like any other ViewModel. <strong>The most important part is determining what is changed in a change detection pass. This is </strong><a href="https://blog.angularindepth.com/a-gentle-introduction-into-change-detection-in-angular-33f9ffff6f10"><strong>change detection in Angular</strong></a><strong> or </strong><a href="https://medium.com/react-in-depth/inside-fiber-in-depth-overview-of-the-new-reconciliation-algorithm-in-react-e1c04700ef6e"><strong>reconciliation in React</strong></a><strong>.</strong></p><h3>How It All Works</h3><p>When looking at the implementation of complex data-structure handling directives such as <a href="https://angular.io/api/common/NgForOf"><strong>NgForOf</strong></a>, <a href="https://angular.io/api/common/NgClass"><strong>NgClass</strong></a> and <a href="https://angular.io/api/common/NgStyle"><strong>NgStyle</strong></a>, it’s easy to observe that to detect changes we need to diff consecutive Input values and apply changes if detected.</p><p>While most frameworks contain some diffing implementation, Angular has been exposing them to developers for us to reuse. There’re two kinds of differs in Angular:</p><ul><li><a href="https://angular.io/api/core/IterableDiffer">IterableDiffer</a>: compare a sequence of items;</li><li><a href="https://angular.io/api/core/KeyValueDiffer">KeyValueDiffer</a>: compare a dictionary;</li></ul><p>While an Angular directive normally only uses one differ (<strong>NgClass</strong> uses either one depends on its Input, but not both), the structure of a Virtual DOM is much more complicated and nested, so both differs need to be used: <strong>KeyValueDiffer</strong> for props and <strong>IterableDiffer</strong> for children. The detailed implementation will not be illustrated here, but conceptually they are just recursive diffing.</p><p>With the knowledge of what is being changed, we need to apply the changes to the View. In Angular there’s an abstract helper for view operations, known as <a href="https://angular.io/api/core/Renderer2">Renderer</a>, with the following shape:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c54c54c2bfc643d3aa3d6c4f0460f8c/href">https://medium.com/media/3c54c54c2bfc643d3aa3d6c4f0460f8c/href</a></iframe><p>As long as we use <strong>Renderer</strong> methods for UI manipulations, any implementation will remain platform-agnostic and can be used in any platform without special handling.</p><p>For rendering Angular components, we need to make use of <a href="https://angular.io/guide/dynamic-component-loader">Angular Dynamic Components</a> functionality, the most important parts <a href="https://angular.io/api/core/ComponentFactory"><strong>ComponentFactory</strong></a> and <a href="https://angular.io/api/core/ComponentFactoryResolver"><strong>ComponentFactoryResolver</strong></a>.</p><p><strong>ComponentFactory</strong> provides the required metadata of Input/Output, making the properties mapping work:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cca8c2bb200838bd3b1c497c77cbffa/href">https://medium.com/media/2cca8c2bb200838bd3b1c497c77cbffa/href</a></iframe><p>In fact, commonly used packages like <a href="https://angular.io/guide/upgrade">ngUpgrade</a> and <a href="https://angular.io/guide/elements">Angular Elements</a> are both built on the top of the Dynamic Components functionality.</p><p>With the ability of Virtual DOM rendering, we still need to let Angular know when to do that. Then comes the life-cycle hook — <a href="https://angular.io/api/core/DoCheck">DoCheck</a>.</p><p>As a specially named hook (all others start with “<em>on</em>” or “<em>after</em>”), <em>DoCheck</em> is designed to handle extra jobs when the change detection runs, which is exactly what we want. So the basic structure of <em>Renderable</em> would be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b1b0892079025b1b782a65d455dee43/href">https://medium.com/media/6b1b0892079025b1b782a65d455dee43/href</a></iframe><p>Now, we can use a <strong>render</strong> method on an Angular component to render another Angular component, nothing magical.</p><h3>The Future Plan</h3><p>While it’s possible to organize Angular components via the <strong>render</strong> method, there&#39;s still plenty of features to do.</p><h4>HTML Attributes support</h4><p>The React API for intrinsic elements is a <a href="https://github.com/facebook/react/issues/13525#issuecomment-417818906">mixture of DOM properties, HTML attributes and renamed DOM properties</a>. (ignoring event system for now)</p><p>While renamed DOM properties are controversial, HTML attributes are definitely needed:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/183fd564f8fd5737132a5386eae24fb5/href">https://medium.com/media/183fd564f8fd5737132a5386eae24fb5/href</a></iframe><p>The <strong>data-</strong> or <strong>aria-</strong> attributes have no corresponding properties, without attributes support, they can only be added by <strong>ref</strong> callbacks (also not supported yet), which isn’t a declarative approach.</p><h4>React/React DOM API Compatibility</h4><p>The concepts and features for Class Component and Function Component are mainly determined by React, while compatibility via alias is a long way to go, lifecycle-hooks and utility functions would still be important for development.</p><h4>Reverse Bridge</h4><p>Use existing Class Components or Function Components in the Angular template without making a Virtual DOM object. Likely, this only makes sense after compatibility support is complete.</p><h4>Global Bootstrap</h4><p>It could be useful to render a Virtual DOM structure without writing any Angular code. This was actually implemented in earlier versions but was removed in a recent refactoring due to the complexity in the build process.</p><p>The basic concept here is: due to no user-defined Angular type, the whole AOT process can be lifted to the build process of the library, and then publishing the AOT-compiled code. The user will no longer need to bring the compiler to the runtime.</p><h3>Summary</h3><p>Templates are great, there’s no need to write expression-based View definition unless having extreme complexity.</p><p>Virtual DOM is technically just a ViewModel and can be handled the same way, there’s no real implementation diversity regardless if it’s being called View or ViewModel.</p><p>The library repo can be found at <a href="https://github.com/trotyl/ng-vdom">on GitHub</a>, while it’s technically not the first release, it’s now no longer a POC and I encourage you to experiment with it (but not recommended for production yet). Feel free to file Bug Report or Feature Request.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60a3be805e59" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-in-depth/introducing-to-ng-vdom-a-new-way-to-write-angular-application-60a3be805e59">Introducing NG-VDOM: A new way to write Angular application</a> was originally published in <a href="https://medium.com/angular-in-depth">Angular In Depth</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>