<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cliff Eby</title>
    <description>The latest articles on DEV Community by Cliff Eby (@cliffeby).</description>
    <link>https://dev.to/cliffeby</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F221667%2Fe409a8f0-84b7-43a2-8a3e-518af0be372b.png</url>
      <title>DEV Community: Cliff Eby</title>
      <link>https://dev.to/cliffeby</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cliffeby"/>
    <language>en</language>
    <item>
      <title>Scopes and Permissions</title>
      <dc:creator>Cliff Eby</dc:creator>
      <pubDate>Thu, 13 Aug 2020 15:46:58 +0000</pubDate>
      <link>https://dev.to/cliffeby/scopes-and-permissions-4hla</link>
      <guid>https://dev.to/cliffeby/scopes-and-permissions-4hla</guid>
      <description>&lt;p&gt;Update Nov 2021 - This content is not dated.  Auth0 has a simple way of dealing with the JWTs roles and scopes.&lt;/p&gt;




&lt;p&gt;TODO - update this &lt;br&gt;
I have a MEAN stack project that uses an Express/Mongoose API to access a MongoDB datastore.  For authorization to endpoints, I used Auth0 and its recommended approach of Scopes which are created, stored, and assigned by API in my Auth0 account.  I used Postman for testing and had several collections to test the API endpoints.  I documented my work at &lt;em&gt;&lt;a href="https://cliffeby.github.io/RochV001a/"&gt;Roch&lt;/a&gt;&lt;/em&gt; with a focus on what should be tested and error reporting.&lt;/p&gt;

&lt;p&gt;On a recent revisit to my tests, I found several failed after I modified a setting in Auth0.  This led to a review of documentation on scopes, roles, and permissions contained in the two most cited standards OAuth 2.0 and OpenID Connect. Beyond the standards, I have not found a recognized authority that explains OAuth 2.0 or OpenID Connect scopes and permissions &lt;sup&gt;1&lt;/sup&gt; .  &lt;/p&gt;

&lt;p&gt;The intent of this note is to document my findings and show the approach that I used to get Auth0 roles and permissions testable and working with my API server &lt;sup&gt;2&lt;/sup&gt; .  Since I expect that most are interested in the latter, I will start with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Auth0 Permissions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Auth0 API
&lt;/h3&gt;

&lt;p&gt;When authenticated by Auth0 scopes and/or permissions are passed to user in the JWT access-token.  The Auth0 API “settings” for your authorized applications allows you to configure the use of Scopes or Permissions.  If Role Based Access Control (RBAC) is selected for the API, you can add permissions to your access token (a second selection in Auth0).  It is worth checking the JWT access token varying these selections to see the changes. (Use JWT.IO to decode the token.) Note that when RBAC permissions are added to the access token, there is no nested “scopes” object in the JWT access-token.&lt;/p&gt;

&lt;h3&gt;
  
  
  My API Server
&lt;/h3&gt;

&lt;p&gt;Per Auth0 endpoint documentation, my API server has a sample endpoint:&lt;br&gt;
&lt;code&gt;router.route('/myMatch').post(jwtCheck, jwtAuthz(['create:match']), matchController.postMatch)&lt;/code&gt; &lt;br&gt;
When scopes were used, this worked fine, but jwtCheck ignored permissions.  The following change corrected the issue and allowed jwtCheck to access the nested permissons in the access-token.&lt;br&gt;
&lt;code&gt;const options = { customScopeKey: 'permissions' };&lt;br&gt;
router.route('/matches').post(jwtCheck, jwtAuthz(['create:match'], options), matchController.postMatch)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  My Angular Client
&lt;/h3&gt;

&lt;p&gt;My client-side Angular app relied on Scopes for route guards and sending authenticated requests to the API server.  Now that Scopes were not present, the app could only access the home screen. I could not find an "options" parameter to make permissions into scopes.  This led to decoding the access-token and I used &lt;code&gt;jwt-decode&lt;/code&gt; to access the nested permissions object.  The permissions were converted to an array of Scopes that my app expected.  The scope array was stored in localStorage (more on this later).  My code is:&lt;br&gt;
&lt;code&gt;const perms = jwt_decode(localStorage.getItem("access_token")); &lt;br&gt;
localStorage.removeItem("scopes");&lt;br&gt;
    var scopes: string = "openid profile";&lt;br&gt;
    for (var p of perms.permissions) {&lt;br&gt;
      scopes = scopes + " " + p; }&lt;br&gt;
    localStorage.setItem("scopes", JSON.stringify(scopes));&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Scopes, Roles and Permissions
&lt;/h3&gt;

&lt;p&gt;I find the Auth0 documentation confusing in describing the difference between Scopes and Permissions.  My puzzlement is likely due to lack of experience and not needing all the features of federated logons and OpenID.  I do not live in the world of authentication and authorization, and that is why I chose Auth0; I did not want to become an expert.&lt;/p&gt;

&lt;p&gt;As mentioned, OAuth 2.0 and OpenID Connect are the current standards for authorization and authentication.  These standards create the ability for a consumer to sign into an app with multiple identities e.g. Facebook, GitHub and a local identity.  The standards also create the ability to use a single sign-on across apps.  &lt;/p&gt;

&lt;p&gt;I reviewed the OAuth 2.0 documentation and found little on permissions.  It focuses on an app’s scopes and a user’s claims.  I also reviewed Azure Active Directory B2C, a competitive product to Auth0 that follows the OAuth 2.0 standard.  It uses a different terminology of claims, user flows, and custom policies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;m3n7alsnak3&lt;/em&gt;&lt;/strong&gt; on Stack Overflow wrote that &lt;em&gt;“Scopes are per Client app, while permissions are per user. In other words - one client app can have a scope(s) to access certain API(s), but the users of this client app will have different permissions in this api (based on their roles).”&lt;/em&gt;  It makes sense to me for the client or API to expect “Scopes” or “Permissions from a user, but why make the distinction?&lt;/p&gt;

&lt;p&gt;For now, I am defining each role in Auth0 to have a specific list of permissions.  The client app extracts the permissions and creates a scope string containing all the expected Scopes.  Reader suggestions or clarification would be appreciated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Local Storage
&lt;/h3&gt;

&lt;p&gt;Local storage aka browser storage was Auth0’s suggested datastore for SPA access tokens and decoded attributes.  Its rationale was that the app needed a secret key that couldn’t be used by another app or person.  Further, scopes, roles, permissions, …  that would be stored in local storage are not unique.  A scope of "read:members" or a role of "admin" is hardly unique to my app and not intended to be a sensitive passcode/secret credential.&lt;/p&gt;

&lt;p&gt;More recently, the general recommendation is to avoid local storage under certain scenarios due to cross site scripting (XSS) vulnerabilities.  Auth0’s recommendation is to use of OAuth’s Authorization Code Flow with Proof Key for Code Exchange (PKCE).&lt;/p&gt;

&lt;p&gt;I will put this on my TODO list.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;1&lt;/a&gt;: This article published in 2017 does a good job of describing OpenID concepts &lt;a href="https://developer.okta.com/blog/2017/07/25/oidc-primer-part-1"&gt;https://developer.okta.com/blog/2017/07/25/oidc-primer-part-1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a&gt;2&lt;/a&gt;: I will update this post with any comments or sources that help to clarify this issue.&lt;/p&gt;

</description>
      <category>auth0</category>
      <category>scopes</category>
      <category>permissions</category>
      <category>mean</category>
    </item>
    <item>
      <title>Initial Nulls in Angular http – Recognition, Debugging and Solutions</title>
      <dc:creator>Cliff Eby</dc:creator>
      <pubDate>Mon, 13 Apr 2020 17:28:02 +0000</pubDate>
      <link>https://dev.to/cliffeby/initial-nulls-in-angular-http-recognition-debugging-and-solutions-384d</link>
      <guid>https://dev.to/cliffeby/initial-nulls-in-angular-http-recognition-debugging-and-solutions-384d</guid>
      <description>&lt;p&gt;I’ve struggled with displaying complex-object Observables in Angular.  When I try to render a variable in my template, sometimes I get nothing, other times errors, combinations of the two, and on occasion, the value with no console errors.  Dealing with null or undefined objects and properties is perplexing particularly with Observables.  Lack of an opinionated way of displaying objects in your template in a desired format is often frustrating.  &lt;/p&gt;

&lt;p&gt;Handling asynchronous calls to back-end data-stores creates a new way of thinking for most.  The evolution from callbacks to promises to observables and their variants (subjects and behavior subjects) has greatly improved the coding experience.  I find that passing asynchronous data to components using a service and observable offers a reliable and easy to grasp concept.  But often I know that I have data, it just doesn’t render.&lt;/p&gt;

&lt;p&gt;Here are few approaches that can work if you have struggled like me.  A working copy of the code below is on Stackblitz at &lt;a href="https://stackblitz.com/edit/angular-7wojgn?embed=1&amp;amp;file=src/app/app-list.component.ts"&gt;https://stackblitz.com/edit/angular-7wojgn&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Part I Auto subscribe
&lt;/h2&gt;

&lt;p&gt;Let’s assume the you have a service injected in your component that returns an observable from an http get.  &lt;/p&gt;

&lt;p&gt;Consider a service method that gets&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;{ "name": "Bob", "roles": [ { "job": "carpenter" }, { "job": "woodworker" } ] }&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 after at least  two seconds;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fetchMembers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://nulls-d2af3.firebaseio.com/members.json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and, component code that calls service and logs the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;mdata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_elementService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ElementService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nx"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mdata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_elementService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fetchMembers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Members&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mdata&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When executed, and looking at the browser console, you see immediately that “mdata” is an observable, but it doesn’t yet have data because I have not yet subscribed to the observable. I can subscribe in the component, but you don’t need to.  You can use the async pipe as an alternative which auto subscribes and unsubscribes for you.&lt;/p&gt;

&lt;p&gt;Example A. Use async to auto subscribe and unsubscribe followed by the json pipe to convert the observable object to a json string.  It will display the entire&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!—&lt;/span&gt;&lt;span class="na"&gt;A.&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;and&lt;/span&gt; &lt;span class="na"&gt;json&lt;/span&gt; &lt;span class="na"&gt;pipe&lt;/span&gt; &lt;span class="na"&gt;--&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
{{mdata | async | json }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;object and is generally an easy way to show that data is available to the template.  Use of &lt;code&gt;{{ mdata | json }}&lt;/code&gt; does not work because there is no subscription.  Use of &lt;code&gt;{{ mdata | async }}&lt;/code&gt; does not work because the object has not been converted to strings for display.&lt;/p&gt;

&lt;p&gt;Example B. Use *ngif and async to display selected data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!—&lt;/span&gt;&lt;span class="na"&gt;B.&lt;/span&gt; &lt;span class="na"&gt;*ngif&lt;/span&gt; &lt;span class="na"&gt;and&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;--&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"mdata | async as mdata; else loading;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;{{mdata.name}} {{mdata.roles[0].job}} {{mdata.roles}} &lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ng-template&lt;/span&gt; &lt;span class="na"&gt;#loading&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Loading User Data...&lt;span class="nt"&gt;&amp;lt;/ng-template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example B will check to see if “mdata” is not null and will display “Loading User Data…” until the observable is resolved.  Note that the async pipe automatically subscribes and unsubscribes to the observable.  Compared to method A, expression interpolation, method B offers better granularity, and if combined with an *ngfor property arrays could display your data in final form.  For this case, the template will, after two seconds, render the following:&lt;/p&gt;

&lt;h4&gt;
  
  
  Bob carpenter [object Object],[object Object]
&lt;/h4&gt;

&lt;p&gt;As shown, &lt;code&gt;{{ mdata.roles }}&lt;/code&gt; is unable to display the json data without a property name.&lt;/p&gt;

&lt;p&gt;If you plan to let the async pipe do the subscribing for you, this is probably a good point to write unit tests for your service and component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part II Self subscribe
&lt;/h2&gt;

&lt;p&gt;Most tutorials on Observables subscribe in the component.  Let’s see how that changes the data in the template.&lt;/p&gt;

&lt;p&gt;Component code that calls the service, subscribes, and logs the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;mdata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_elementService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ElementService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nx"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_elementService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fetchMembers&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mdata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Members&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mdata&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing to notice when executed is Members (in the browser console) shows the asynchronous call when Members’ properties populate after two seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!—&lt;/span&gt;&lt;span class="na"&gt;C.&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;and&lt;/span&gt; &lt;span class="na"&gt;json&lt;/span&gt; &lt;span class="na"&gt;pipe&lt;/span&gt; &lt;span class="na"&gt;--&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
{{mdata | json }}

&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!—&lt;/span&gt;&lt;span class="na"&gt;D.&lt;/span&gt; &lt;span class="na"&gt;*ngif&lt;/span&gt; &lt;span class="na"&gt;and&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;--&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

 &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"mdata; else loading;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="err"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;mdata.name&lt;/span&gt;&lt;span class="err"&gt;}}&lt;/span&gt; &lt;span class="err"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;mdata.roles&lt;/span&gt;&lt;span class="err"&gt;[0].&lt;/span&gt;&lt;span class="na"&gt;job&lt;/span&gt;&lt;span class="err"&gt;}}&lt;/span&gt; &lt;span class="err"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;mdata.roles&lt;/span&gt;&lt;span class="err"&gt;[0]}}&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="na"&gt;h2&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ng-template&lt;/span&gt; &lt;span class="na"&gt;#loading&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Loading User Data...&lt;span class="nt"&gt;&amp;lt;/ng-template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Examples C and D, we remove the async pipe and output identical values to Part I.  But since we’re subscribed, we now have other options.&lt;/p&gt;

&lt;h3&gt;
  
  
  Part III – Dealing with nulls – Safe-Navigation and Elvis operators
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!—&lt;/span&gt;&lt;span class="na"&gt;E.&lt;/span&gt; &lt;span class="na"&gt;Value&lt;/span&gt; &lt;span class="na"&gt;of&lt;/span&gt; &lt;span class="na"&gt;an&lt;/span&gt; &lt;span class="na"&gt;array&lt;/span&gt; &lt;span class="na"&gt;property&lt;/span&gt; &lt;span class="na"&gt;--&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
{{mdata.name }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example E accesses the observable’s description property.  When executed Bob renders on the page after two seconds.  Everything seems fine until you look at the console.  It is littered with errors saying “ERROR TypeError: Cannot read property '0' of undefined.”  These errors are produced before the observable is resolved and the console can’t say “Oh, never mind.”&lt;/p&gt;

&lt;p&gt;To stop the errors, let’s try the Elvis operator.  Use of the Elvis operator is saying get someProperty on someObject if someObject exists.  It’s form is objectname?.property and can deal with deeply nested properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!—&lt;/span&gt;&lt;span class="na"&gt;F.&lt;/span&gt; &lt;span class="na"&gt;Value&lt;/span&gt; &lt;span class="na"&gt;of&lt;/span&gt; &lt;span class="na"&gt;an&lt;/span&gt; &lt;span class="na"&gt;array&lt;/span&gt; &lt;span class="na"&gt;property&lt;/span&gt; &lt;span class="na"&gt;with&lt;/span&gt; &lt;span class="na"&gt;Elvis&lt;/span&gt; 
&lt;span class="err"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;mdata&lt;/span&gt;&lt;span class="err"&gt;?.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="err"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It too renders Bob, and the console errors are gone.  But what about &lt;code&gt;{{ mdata?roles?.job }}&lt;/code&gt;? Turns out that in Angular the Elvis operator works well on nested properties and stops errors when an array is present, but it doesn’t display any data or the "object, object" notation.  The Elvis operator works well for non-arrayed complex objects but could be misleading.  I find that most of my data starts as an array of objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!—&lt;/span&gt;&lt;span class="na"&gt;G.&lt;/span&gt; &lt;span class="na"&gt;Value&lt;/span&gt; &lt;span class="na"&gt;of&lt;/span&gt; &lt;span class="na"&gt;an&lt;/span&gt; &lt;span class="na"&gt;array&lt;/span&gt; &lt;span class="na"&gt;property&lt;/span&gt; &lt;span class="na"&gt;expression&lt;/span&gt;
&lt;span class="err"&gt;{{&lt;/span&gt;  &lt;span class="na"&gt;mdata&lt;/span&gt; &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="na"&gt;mdata.name&lt;/span&gt; &lt;span class="err"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Interpolation in Angular &lt;code&gt;{{ expression }}&lt;/code&gt;  evaluates the expression and we can use that evaluation to check for nulls or undefined.   The table below shows some possibilities&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;HTML&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;th&gt;Comment&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{{ mdata &amp;amp;&amp;amp; mdata.roles }}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;[object Object]&lt;/td&gt;
&lt;td&gt;No console errors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{{ 1 &amp;amp;&amp;amp; 2 }}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;No console errors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{{ 1 OR 2 }}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;No console errors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{{ 3 == 3 }}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;td&gt;No console errors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{{ “No data” &amp;amp;&amp;amp; mdata.roles[1].job }}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;woodworker&lt;/td&gt;
&lt;td&gt;Console errors –  Cannot read property [roles] - types do not match&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{{ mdata &amp;amp;&amp;amp; mdata.roles[1].job }}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;woodworker&lt;/td&gt;
&lt;td&gt;No console errors – types match&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Part IV – Resources
&lt;/h3&gt;

&lt;h4&gt;
  
  
  A good article by Todd Moto
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://ultimatecourses.com/blog/angular-ngif-async-pipe"&gt;https://ultimatecourses.com/blog/angular-ngif-async-pipe&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>nulls</category>
      <category>observable</category>
      <category>http</category>
    </item>
    <item>
      <title>Understanding Angular Material Table Inputs</title>
      <dc:creator>Cliff Eby</dc:creator>
      <pubDate>Wed, 02 Oct 2019 23:50:46 +0000</pubDate>
      <link>https://dev.to/cliffeby/understanding-angular-material-table-inputs-1lp</link>
      <guid>https://dev.to/cliffeby/understanding-angular-material-table-inputs-1lp</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmoriohcdn.b-cdn.net%2F5d0f1134f9.png" class="article-body-image-wrapper"&gt;&lt;img alt="Angular Table" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmoriohcdn.b-cdn.net%2F5d0f1134f9.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Background&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;About a month ago, I created my first Angular Material Table.  Things went well and the implementation of filtering, sorting, and paging was as described by the docs.  Several tables into my project , I had to shape my table input data.  Probably due to inexperience with Observables and/or the use of EventEmitters, I began to struggle.  The docs have an async http example, but it didn’t address shaping and sharing table data between components.&lt;br&gt;
Searches for examples showed frustration among some.   Often the input data issue was obscured by Material Table features that were unrelated to the problem.  Also, I didn’t find any recommended practice for managing table input data, so I created this &lt;a href="https://stackblitz.com/edit/angular-trsxts" rel="noopener noreferrer"&gt;StackBlitz&lt;/a&gt; to see if I could better understand options and preferences.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;table mat-table&amp;gt;&lt;/code&gt; directive creates a Material Design styled data-table.  Directive mat-table has a dataSource input which can be an array, an Observable array, or a DataSource instance.  A &lt;code&gt;&amp;lt;table mat-table [dataSource] = simple_array&amp;gt;&lt;/code&gt; will produce a static material-styled table.  Changes to the simple array will not be propagated to the table.  If &lt;code&gt;&amp;lt;table mat-table [dataSource] = Observable&amp;lt;any[]&amp;gt;&lt;/code&gt;, then changes to the observable array are propagated to the table.  Finally, if &lt;code&gt;&amp;lt;table mat-table [dataSource] = DataSource instance&amp;gt;&lt;/code&gt;, then changes, sorting, filtering, pagination, etc. are encapsulated in the class instance for easy access.  This instance can be created by passing an array or Observable array as follows: &lt;code&gt;instance = new MatTableDataSource(array)&lt;/code&gt;.&lt;br&gt;
The &lt;a href="https://stackblitz.com/edit/angular-trsxts" rel="noopener noreferrer"&gt;StackBlitz&lt;/a&gt; shows the creation of Material Tables using:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; static data – simple array,&lt;/li&gt;
&lt;li&gt; a service property – DataSource Instance&lt;/li&gt;
&lt;li&gt; a service Observable,&lt;/li&gt;
&lt;li&gt; a service Observable in a child component – DataSource Instance,&lt;/li&gt;
&lt;li&gt; a child component emitting to a function – DataSource Instance.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Get Started – Open the &lt;a href="https://stackblitz.com/edit/angular-trsxts" rel="noopener noreferrer"&gt;StackBlitz&lt;/a&gt; to follow along&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I used the Material Table “Basic use of” example at &lt;a href="https://material.angular.io/components/table/examples" rel="noopener noreferrer"&gt;https://material.angular.io/components/table/examples&lt;/a&gt; as a starting point.   Other than the change to the &lt;code&gt;[dataSource]&lt;/code&gt; input, the sample html was not modified. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Data are provided by an array const directly in app.component.  Table No. 1 shows this implementation.     Other than moving the data model interface to an external class, nothing differs from the “Basic use of” sample.&lt;/li&gt;
&lt;li&gt; In most applications, data come from a service.  I created &lt;code&gt;ElementService&lt;/code&gt; in dataService.ts with a &lt;code&gt;listElements&lt;/code&gt; property and a &lt;code&gt;getElements()&lt;/code&gt; method.  Table No. 2 uses the property accessor to create the table.  As with Table No.1, it is a synchronous implementation.&lt;/li&gt;
&lt;li&gt; Typically, data from a service are async using http.  I mimicked this behavior with an Obervable.delay.  &lt;code&gt;timedelay&lt;/code&gt; is a variable to populate each async table sequentially.  Table No. 3 subscribes to the Observable created in the service with a two second delay.  On refresh, you will see this table populate after two seconds.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tables No. 4a and 4b are examples of shaping the data input in a child component.  You can pass either the observable function or the response of subscribing to the observable.  I chose to pass the function and subscribed to it in the child component.  The function is passed in the app.component.html as &lt;code&gt;[listElementChild] = ‘elsService’&lt;/code&gt;.  In VS Code, hovering over these objects provides the component name.  &lt;code&gt;[listElementChild]&lt;/code&gt;, a service function, is passed to the child.component using the @Input() decorator.  In &lt;code&gt;ngOnInit&lt;/code&gt;, the observable is subscribed to and then new objects/records are added to the observable array.&lt;br&gt;&lt;br&gt;
Table No 4a. uses the child.component.html and subscribes to the passed observable function.  A new element (Beryllium) is pushed onto the response and that response is set and the mat-table’s dataSource.&lt;br&gt;&lt;br&gt;
Table No. 4b adds a fifth element (Boron) and creates a &lt;code&gt;DataSource instance&lt;/code&gt; as the table’s input and uses the child.component.html.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Table No.5 creates the shaped observable and emits it via the Output() decorator to the parent.  The event is being watched in app.component.html by &lt;code&gt;(dsEvent) = 'onChildDS($event)'&lt;/code&gt;.  When &lt;code&gt;dsEvent&lt;/code&gt; emits, the &lt;code&gt;onChildDS()&lt;/code&gt; method creates a new &lt;code&gt;DataSource instance&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>angular</category>
      <category>materialtable</category>
      <category>datasource</category>
      <category>input</category>
    </item>
    <item>
      <title>Complex-Object Change Detection in Angular</title>
      <dc:creator>Cliff Eby</dc:creator>
      <pubDate>Mon, 23 Sep 2019 21:00:27 +0000</pubDate>
      <link>https://dev.to/cliffeby/complex-object-change-detection-in-angular-25k</link>
      <guid>https://dev.to/cliffeby/complex-object-change-detection-in-angular-25k</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd6vdma9166ldh.cloudfront.net%2Fmedia%2Fimages%2Fbd9734c9-def0-47ee-b9ec-027fcfe3cae8.png" class="article-body-image-wrapper"&gt;&lt;img alt="Angular Logo" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd6vdma9166ldh.cloudfront.net%2Fmedia%2Fimages%2Fbd9734c9-def0-47ee-b9ec-027fcfe3cae8.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’m working on an Angular app that has several parent-child components.  Things were going well using the @Input() and @Output() decorators to pass data between components.  When I started using Angular Material tables for data display, my data object increased in complexity.  Suddenly, I was getting inconsistent behavior in the view.  Sometimes data were updated; other times partially, or not at all.  &lt;/p&gt;

&lt;p&gt;Researching the “change detection loop” and posts on how to force or limit change detection, offered little toward solving my problem.  Blogs on zones, state change triggers, and DOM trees were informational, but none explained the inconsistent behavior.  After hundreds of console.logs and breakpoints, I spotted my problem.  &lt;strong&gt;Object properties that hold an array reference only update when another value property in the object changes.&lt;/strong&gt;   I suspect that I read something like that in a post, but to a non computer-science major, it went over my head.&lt;/p&gt;

&lt;p&gt;Let’s look at an example. My Angular data model is defined in class Match:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Match&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;playerNames&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;My app component creates an instance of Match which has three methods:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; addName - To add a name to the playerNames array.&lt;/li&gt;
&lt;li&gt; changeMatchName - To change the match name, and &lt;/li&gt;
&lt;li&gt; spread - To be discussed later.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chg-detection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Match&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nl"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playerNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Match 0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;addName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playerNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playerNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;changeMatchName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Match &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;spread&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playerNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playerNames&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Chuck&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playerNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The html is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"text-align:center"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;
      Welcome to {{ title }}!
    &lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"300"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"Angular Logo"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://d6vdma9166ldh.cloudfront.net/media/images/bd9734c9-def0-47ee-b9ec-027fcfe3cae8.png"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;1: {{match.name}} Player names: {{match.playerNames}} # of players :{{match.playerNames.length}}&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;2: {{match.name}} Player names: {{match.playerNames}} &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;  &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"primary"&lt;/span&gt; &lt;span class="na"&gt;(click)=&lt;/span&gt;&lt;span class="s"&gt;"addName()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Add Player
  &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;  &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"primary"&lt;/span&gt; &lt;span class="na"&gt;(click)=&lt;/span&gt;&lt;span class="s"&gt;"changeMatchName(counter)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Change Match Name
  &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;  &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"primary"&lt;/span&gt; &lt;span class="na"&gt;(click)=&lt;/span&gt;&lt;span class="s"&gt;"spread()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Spread
  &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that &lt;strong&gt;div line 1, contains a value property length that is not included in div line 2&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;When the app initiates, it ouputs:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;1: Match 0  Player names:  # of players :0&lt;/p&gt;

&lt;p&gt;2: Match 0  Player names:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step1: I add a player, line 1 adds the player name but Line 2 does not.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;1: Match 0  Player names: Bob0  # of players :1&lt;/p&gt;

&lt;p&gt;2: Match 0  Player names:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 2: I change the match name.  Both lines 1 and 2 update player names to the current state of the Match object&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;1: Match 1  Player names: Bob0  # of players :1&lt;/p&gt;

&lt;p&gt;2: Match 1  Player names: Bob0&lt;/p&gt;

&lt;h2&gt;
  
  
  So what is happening?
&lt;/h2&gt;

&lt;p&gt;Since the addName() method is &lt;strong&gt;pushing&lt;/strong&gt; a value on the match.playerNames array, the reference value of the array is not changed.  Only on Line 1 where the “value” of the array length is changed in the view, does Angular’s change detection loop update that div of interpolated expressions.  So not only do the component and its view have different change detection loops, but the view is granular in what gets updated. That results in Line 1 with current values of player names, and Line 2 with stale values.&lt;/p&gt;

&lt;p&gt;In Step 2, changing the match’s name, which is assigned by value, creates a change detection cycle on both div lines resulting in both lines showing the current state of players.&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://stackblitz.com/edit/angular-sa1un1" rel="noopener noreferrer"&gt;StackBlitz&lt;/a&gt; link provides a working demo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this inconsistency?
&lt;/h2&gt;

&lt;p&gt;For most apps, I would expect that the current values of an object’s properties are what is anticipated in the view. I have not seen an explanation of why array or other reference objects are not part of change detection, but I suspect it is performance based since navigating the entire tree and especially big arrays can be expensive.  In support of this conclusion, Angular does offer a “ChangeDetectionStrategy.OnPush” strategy to limit change detection to only part of the component tree.  The rationale for .onPush is performance improvement.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;Several options are possible to force change detection on a reference value.  They all rely on the Angular change detection principle that &lt;strong&gt;new&lt;/strong&gt; objects are always updated.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; An ngrx approach with a Redux store.&lt;/li&gt;
&lt;li&gt; Use of immutable.js&lt;/li&gt;
&lt;li&gt; Use of the ES6 spread operator.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I chose the spread operator as it seemed it is the easiest to implement, to understand, and it is native to javascript.  The spread operator has the form data = {…data, new} where new replaces or adds values to the existing data object and creates a &lt;strong&gt;new&lt;/strong&gt; object value.  More on spread can be found &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In my example the:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playerNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Bob&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="nx"&gt;becomes&lt;/span&gt;

&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playerNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;playerNames&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chuck&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Another approach would be to use a service to retrieve the data object.  In my case, the server data model uses a player id property in match to reference all player attributes.  Including playerNames in that model would add redundant data to the backend datastore or would create a complex angular service using Local Storage.&lt;/p&gt;

&lt;p&gt;Read more &lt;a href="https://itnext.io/dont-clone-back-end-models-in-angular-f7a749bdc1b0" rel="noopener noreferrer"&gt;here&lt;/a&gt;  &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
