3

I was originally trying to set up a template reference variable in Angular2 so I could reverse the sort order of a table without having to do the binding but I wasn't getting the dynamic updating of the interface when I was clicking the checkbox. I created a simple plunker in case I had something else in my app that might have been messing with the code and I don't see the interpolated value of the reference being updated on the fly. I made it a textbox to see if the value would change as I typed. It doesn't appear to be updating.

Am I missing something in my setup to make this work? Or am I trying to use the reference variables in a way they weren't created to be used for?

http://plnkr.co/edit/BK4MzU8KTvEyXDxTNHSR?p=preview

import {Component} from '@angular/core'

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2> 
       <input #myInput type="text" value="yeh" /> The INPUT value is "{{myInput.value}}"
    </div>
  `,
  directives: []
})
export class App {
  constructor() {
    this.name = 'Angular2 (Release Candidate!)'
  }
}

2 Answers 2

5

With <input #myInput type="text" value="yeh" /> you create a local template reference to DOM element, however it doesn't mean Angular watches it's value or any other arbitrary property, like checked, etc.

If you need to watch value you should better use ngModel or ngControl (see another answer (which is deleted now: <input [(ngModel)]="myInput" type="text" value="yeh" />)).

Another solution is to trigger "watching" by subscribing to some of the input events, e.g. keyup or input.

<input #myInput type="text" (input)="false" value="yeh" /> {{ myInput.value }}

and use some "event handler" or fake one, like false. As you can see this is really weird, avoid it.

Demo: http://plnkr.co/edit/JIwPDPyYFnkDtz99sew3?p=info

Sign up to request clarification or add additional context in comments.

1 Comment

I figured this might be the case but wasn't 100% sure of exactly how the reference was supposed to work here. Thanks for the clarification.
0

Try this-

<input #myInput type="text" value="yeh" (keyup)="myInput.value" /> The INPUT value is "{{myInput.value}}"

See if this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.