10

I am trying to do a simple binding from an Angular2 component to the template. My template code looks like this...

<textarea class="left-side" #newLeft (keyup)="enterLeftText($event, newLeft)"></textarea>

<textarea class="right-side">{{leftText}}</textarea>

Then in my component I have the following...

enterLeftText($event, newLeft) {
  this.leftText = newLeft.value;
}

The problem is that newLeft is always undefined. What am I missing?

3
  • 2
    Looks like textarea doesn't support the #binding notation. Maybe not implemented yet, you'll need to create you own directive to watch for events and update the model Commented Apr 23, 2015 at 18:23
  • changed it to input plnkr.co/edit/dtbDwneFFIiTazjiF7Li?p=preview still no luck Commented Apr 23, 2015 at 18:36
  • you're right, the error was elsewhere, see my answer Commented Apr 23, 2015 at 18:47

2 Answers 2

9

You found an interesting bug, as it seems we cannot have an uppercase in a #id binding.

Simply replacing newLeft with newleft will solve your problem:

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


UPDATE: in fact it appears that the id should be dash-noted and the variable is camel case, as it is the case on Angular 1 when binding attributes.

So the real answer to your problem is to write #new-left:

<textarea class="left-side" #new-left (keyup)="enterLeftText($event, newLeft)"></textarea>
Sign up to request clarification or add additional context in comments.

3 Comments

crazy syntax =) in comparison to aurelia's <textarea class="left-side" value.bind="leftText"></textarea>
@arebutuv that syntax is not equivalent to the code given. He wants to execute a function on key up.
Templates are now camelCase (since late beta) and #newLeft should work fine.
0

When using "#new-left" I get an Exception

EXCEPTION: Error: Uncaught (in promise): Template parse errors: "-" is not allowed in variable names

The solution with camelCased variable names works fine now for me.

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.