Using ng-transclude doesn't seem to work well in a table

后端 未结 1 1503
生来不讨喜
生来不讨喜 2021-01-17 11:40

I created a simple directive that is used to display some text in a when there is no table data (i.e. \"No results found\" ) takes up the entire row

1条回答
  •  迷失自我
    2021-01-17 12:10

    I believe the reason you are seeing this is not because of Angular but rather the browser seeing that it is invalid html inside a as it is expecting and as a result it moves this content to above the table BEFORE Angular even gets a chance to run and do the transclusion. You can easily test this by removing any Angular code and just leave the HTML and you'll notice the outcome is exactly the same.

    Here is a workaround that you might be able to use:

    
      Here is my text I want to transclude into my directive
    
    

    And the directive:

    app.directive('skNoResult', ['$rootScope', function () {
      return {
        restrict: 'A',
        replace: true,
        transclude: true,
        template: '
    ', link: function (scope, elem, attrs) { var span = angular.element(elem).parents('tbody').siblings('thead').find('tr').children().length; scope.colSpan = span; } }; }])

    Note that the element usage of ngTransclude, i.e. is only available from Angular version 1.3.0-beta.16 and above. If you are using a 1.2 release you need to use the attribute usage as in the example above

    Here is a working demo.

    0 讨论(0)
提交回复
热议问题