Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

glenn romaniuk
glenn romaniuk
32,520 Points

How to change an angular variable from ng-keyup

I have the following ng-keyup directive Want to change the value of the editing property when the enter key is pressed The value changes in the controller but does not change in the model I suspect this is not passed in by ref

<input ng-change="todo.edited = true" ng-blur="editing = false;" ng-show="editing" ng-model="todo.name" class="editing-label" type="text" ng-keyup="test($event, editing)" />

$scope.test = function(event, editing){ console.log(event.keyCode); console.log(editing); if(event.keyCode === 13) { console.log("enter key presses"); editing=false; } };

2 Answers

Vance Rivera
Vance Rivera
18,322 Points

you can change the ng-keyup to assign the return of the method test to the edit property. Hope this helps. Cheers!

<input ng-change="todo.edited = true" ng-blur="editing = false;" ng-show="editing" ng-model="todo.name" class="editing-label" type="text" ng-keyup="editing = test($event)" />
$scope.test = function(event){ 
                         console.log(event.keyCode); 
                         console.log(editing); 
                          if(event.keyCode === 13) 
                           { 
                               console.log("enter key presses"); 
                               return false; 
                            } else return true; 
 };
glenn romaniuk
glenn romaniuk
32,520 Points

I'm new to angular. Thx for the tip!