Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS – append row after element in directive

My question is similar as this one, but instead of prepending row, I want it to append.

This doesn’t work:

app.directive('createTable', function ($compile) {
  return {
    link: function (scope, element, attrs) {
      var contentTr = angular.element('<tr><td>test</td></tr>');
      contentTr.parentNode.insertBefore(element, contentTr.nextSibling);
      $compile(contentTr)(scope);
    }
  }
});
like image 396
kuboslav Avatar asked Aug 28 '13 08:08

kuboslav


2 Answers

This does the job:

app.directive('createTable', function ($compile) {
    return {
        link: function(scope, element, attrs) {
            if (element.next().length) {
                element.next().insertBefore(element);
            }

            var contentTr = angular.element('<tr><td>test</td></tr>');
            contentTr.insertAfter(element);
            $compile(contentTr)(scope);
        }
    }
});

http://jsfiddle.net/3gt9J/3/

like image 50
noj Avatar answered Sep 20 '22 12:09

noj


I think you need

app.directive('createTable', function ($compile) {
    return {
        link: function (scope, element, attrs) {
            var contentTr = angular.element('<tr><td>test</td></tr>');
            contentTr.insertAfter(element);
            $compile(contentTr)(scope);
        }
    }
});

Demo: Fiddle

like image 26
Arun P Johny Avatar answered Sep 18 '22 12:09

Arun P Johny