title: Using Angular 2 two-way data binding with Polymer
body: <p>I'm trying to combine Polymer elements with Angular 2 and I struggle with the two-way data binding aspect. </p>
  
  <p>As an example I use paper-tabs and display the tab number that is currently in use. Whenever I change a tab, I want that change to happen in a tabNr field in the AppComponent as well. Whenever I click on the tab number that is being displayed, I want the number to change and change the tab accordingly. My initial approach was this:</p>
  
  <pre><code>import {Component} from 'angular2/core';
  
  @Component({
      selector: 'my-app',
      template: `
                  &lt;paper-toolbar&gt;
                      &lt;paper-tabs [(selected)]="tabNr"&gt;
                          &lt;paper-tab&gt;Tab 1&lt;/paper-tab&gt;
                          &lt;paper-tab&gt;Tab 2&lt;/paper-tab&gt;
                          &lt;paper-tab&gt;Tab 3&lt;/paper-tab&gt;
                      &lt;/paper-tabs&gt;
                  &lt;/paper-toolbar&gt;
                  &lt;paper-toolbar&gt;
                      &lt;div (click)="traverse()"&gt;Tab {{tabNr + 1}}&lt;/div&gt;
                  &lt;/paper-toolbar&gt;
                  `
  })
  export class AppComponent {
      tabNr = 0;
  
      traverse() {
          this.tabNr = (this.tabNr + 1) % 3;
      }
  
  }
  </code></pre>
  
  <p>However, this does not work. I can't change the tabs. I can click the Tab number being displayed to change that, but that is also not reflected in the actual tabs. In the following I'll focus on that line in the code to discuss all other possibilities I tried out:</p>
  
  <pre><code>&lt;paper-tabs [(selected)]="tabNr"&gt;
  </code></pre>
  
  <p>I was expecting that one way data-binding (paper-tabs going to the AppComponent) would work like this</p>
  
  <pre><code>&lt;paper-tabs (selected)="tabNr"&gt;
  </code></pre>
  
  <p>but that doesn't work. To actually get the changes to register, I need to use the event from Polymer like this:</p>
  
  <pre><code>&lt;paper-tabs selected="tabNr" (selected-changed)="tabNr=$event.detail.value"&gt;
  </code></pre>
  
  <p>That defeats the purpose of using Angular 2 for the bindings. It seems to me that the problem here is, that polymer is firing a different event for the change than whatever Angular 2 is expecting.</p>
  
  <p>In order to get the click-changes to register with the tabs I have to do this:</p>
  
  <pre><code>&lt;paper-tabs [attr.selected]="tabNr"&gt;
  </code></pre>
  
  <p>However, I cannot combine both of those result into this</p>
  
  <pre><code>&lt;paper-tabs [attr.selected]="tabNr" (selected-changed)="tabNr=$event.detail.value"&gt;
  </code></pre>
  
  <p>because I get the following exception whenever I change a tab:</p>
  
  <blockquote>
    <p>EXCEPTION: Expression 'tabNr in AppComponent@2:17' has changed after
    it was checked. Previous value: '1'. Current value: '1' in [tabNr in
    AppComponent@2:17]</p>
  </blockquote>
  
  <p>I assume there is probably some sort of circular event firing going on. I can solve this by using a method for the (selected-changed) event that only changes it if the number of the event is actually a different number, but that whole approach seems cumbersome and I derived quite a bit from simply using [()] to bind the data.</p>
  
  <p>So my question is:
  Is there a better, cleaner way to do this?</p>