Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS - ngRepeat. Need to keep focus on the same element event after ng-repeat re rendering
    primarykey
    data
    text
    <p>I have a list of three inputs, each of them can be deleted by the user by pressing a red cross placed at the left side of them. When the user delete one input, the next one is focused and the selected checkbox is deleted. Until there so far so good. After deleting the element the ng-repeat loop iterates over the updated model and I lose the element focus. Is there any way to prevent this?</p> <p>Thanks so much,</p> <p>Guillermo</p> <p>PD: Including the directive code just in case.</p> <pre><code>directive('checkEmptyInput', function ($timeout,$q) { return { restrict: 'A', require: '?ngModel', link: function (scope, element, attrs, ngModel) { if(!ngModel) return; //do nothing if no ng-model var idx = scope.$index, array = [], fieldIsValid = false; element.on('keyup', function () { switch (attrs.checkEmptyInput) { case "method": array = scope.album.tracks; fieldIsValid = scope.ngAlbumForm.track.$valid; break; } if(array[idx].name &amp;&amp; array[idx].name != "" &amp;&amp; fieldIsValid) { array[idx].active = true; } else { array[idx].active = false; if(scope.elementHasId(array[idx])){ scope.deleteKeyFromCombinationsCodes(array[idx].id); } }; if (array[idx].name == "") { //Previous to delete focus the next element $timeout(function () { var nextElem = element.parent().next().find('input')[0]; if(nextElem !== undefined) { if(!scope.$last) { //Delete the element console.log("Deleting from inside the directive..."); nextElem.focus(); scope.$emit("deleteArrayElement", [idx]); } } }); } }); } } }) </code></pre> <p>HTML:</p> <pre><code> &lt;div ng-repeat="track in album.tracks"&gt; &lt;input check-empty-input="track" type="text" ng-change="addInputText($index,$last,track.name,album.tracks;" ng-minlength="3" class="album_tracks" ng-model="track.name" ng-unique="track"/&gt;&lt;button type="button"/&gt; &lt;i class="remove-icon"&gt;&lt;/i&gt; &lt;/button&gt; &lt;/div&gt; </code></pre> <p>Plunker: <a href="http://plnkr.co/edit/BRN9csBcxHp5QU96NKq9?p=info" rel="nofollow">http://plnkr.co/edit/BRN9csBcxHp5QU96NKq9?p=info</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload