Skip to content Skip to sidebar Skip to footer

Directive Doesn't Fire After Changing Textarea Model

I have a text with newline separator and URLs: first row\nFind me at http://www.example.com and also\n at http://stackoverflow.com. I want to update ng-repeat values after pressing

Solution 1:

Just remove the track by $index from your ng-repeat. This is because you are telling Angular that the value of note_value.split('\n') will only be changed when there is a change in the $index i.e. size of the array after splitting by new line.

But the default implementation of track by is the identity of each item. So when you changed the default implementation to track it by the $index and when you are not not adding a new line instead just updating the content of any existing line, Angular is not able to detect that there is a change.

Update

Removing the track by $index function will throw an exception when there are same values after split. So you can use a simple function like: (define it in your controller)

$scope.indexFunction = function($index, val) {
    // Creating an unique identity based on the index and the valuereturn$index + val;
};

And then use it in your ng-repeat like:

<p ng-repeat="row in note_value.split('\n') track by indexFunction($index, row)"></p>

https://docs.angularjs.org/api/ng/directive/ngRepeat

Post a Comment for "Directive Doesn't Fire After Changing Textarea Model"