Dan Caragea
Dan Caragea

Reputation: 1794

angularjs same textarea to edit multiple elements

I have this case where I set a current item from a list and I need to use a textarea to edit that element's value. Because of some constraints I have to use a keyup event but I don't think that's a problem.

Check this fiddle: http://jsfiddle.net/terebentina/Euj2C/

  1. click on first/second buttons - it works, it changes the text in the textarea to the value of each element.
  2. change the text in the textarea to something
  3. click on the first/second buttons again - the textarea is not updated anymore. If you look in console, you can see that it switches between the elements, it's just that the textarea is not updated anymore.

Any suggestions? this is driving me nuts!

Upvotes: 0

Views: 1178

Answers (1)

Gloopy
Gloopy

Reputation: 37785

I know there is a better way modifying your directive to do this but as a quick fix for now you can try binding your textarea to a ngModel value that is just a copy of the current text in the selected element:

<textarea keyup="" ng-model="keyupText"></textarea>

With this in as your current function:

$scope.current = function(idx) {        
    $scope.current_element = $scope.elements[idx];
    $scope.keyupText = $scope.current_element.value;
    console.log('current is', $scope.current_element.value);
}

See this fiddle for an example.

Upvotes: 1

Related Questions