0

The table's content is loaded by ajax and I want to show loading content like this. How can I turn these two ng-show into directive

<table>
    <tr>
        <th>aaa</th>
        <th>bbb</th>
        <th>ccc</th>
    </tr>
    <tr ng-show="isLoading">Loading...</tr>
    <tr ng-show="!isLoading" ng-repeat="log in logs">
        <td>{{log.aaa}}</td>
        <td>{{log.bbb}}</td>
        <td>{{log.ccc}}</td>
    </tr>
</table>
3
  • 2
    What exactly would you want directive to do? Seems like over complicating something that is already really simple, easy to read and is core functionality. Also note that <table> is very limited on allowed types of child elements so you can't use a custom tag as a child of <table> Commented May 29, 2016 at 5:20
  • Actually I'd like to make a directive like <table><tr>...</tr><tr loading ng-repeat="xxx"></tr></table> Commented May 29, 2016 at 6:07
  • Well that is quite different from question asking only how to turn ng-show's into directive. Would likely help for you to read How to Ask. being as detailed and specific as possible avoids confusion and guessing on our part. Asking a good question does take a bit of effort but will also get much quicker response Commented May 29, 2016 at 6:11

1 Answer 1

1

tableDirective:

angular.module('app', [])
  .directive('tableDirective', function() {
    return {
      restrict: 'E',
      scope: {
        isLoading: '=',
        logs: '='
      },
      templateUrl: 'table.html'
    }
});

table.html:

<table>
    <tr>
        <th>aaa</th>
        <th>bbb</th>
        <th>ccc</th>
    </tr>
    <tr ng-show="isLoading">Loading...</tr>
    <tr ng-show="!isLoading" ng-repeat="log in logs">
        <td>{{log.aaa}}</td>
        <td>{{log.bbb}}</td>
        <td>{{log.ccc}}</td>
    </tr>
</table>

view:

<table-directive is-loading="isLoading" logs="logs"></table-directive>

isLoading and logs from your controller.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.