4

I have a form template as shown below.

<form editable-form name="tableForm">
    <table>
       <tr style="font-weight: bold">

           // Column names here....

       </tr>
       <tr ng-repeat="user in list1>

          //Column values here ...

       </tr>
    </table>
</form>

I want to reuse this template for creating multiple table within same page/controller using different model e.g. list1, list2, list3 etc.

How can I pass the name of the form and model dynamically so that all these tables's operation works independently of each other and still I need not repeat same html with different form name and list model inside my view.html?

Thanks

2
  • 2
    Take a look in directive. Commented Sep 5, 2016 at 10:40
  • Thanks @StepanKasyanenko. I am looking into that. Commented Sep 5, 2016 at 11:21

2 Answers 2

4

You can add a directive to Your angular application.

First, You have to move Your html to a separate file, then declare a directive:

.directive('tableform', function() {
  return {
    scope: {
      list: '='
    },
    restrict: 'E',
    replace: true,
    templateUrl: '<path to html file>'
  };
});

and, finally, You can use it like this:

<tableform list="list1" />
<tableform list="list2" />
...
Sign up to request clarification or add additional context in comments.

Comments

0

In Controller:

scope.dataLists = [ [list1], [list2], [list3] ]

Main View:

<form editable-form name="tableForm">
    <div ng-repeat="list in dataLists" ng-include="'reuseable-table.html'"></div>
</form>

reuseable-table.html:

<table>
  <tr style="font-weight: bold">

  </tr>
  <tr ng-repeat="user in list">
  
  </tr>
</table>

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.