0

I have a table in a template I want to populate with different data. My approach is using directives in Angular. I managed to make a template out of my table but I have no idea how to apply the value for the ng-repeat attribute from my html.

Here's a part of my index.html

<div id='unannounced' kc-item-table>
</div>

And here's a part of my template

<table class='table'>
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat='item in changableItemList'>
            <td>{{item.name}}</td>
            <td>{{item.description}}</td>
        </tr>
    </tbody>
</table>

Heres my directive

app.directive('kcItemTable', function() {
    return {
        restrict: 'E',
        templateUrl: 'scripts/controllers/itemTableTemplate.html'
    }
})

So in order to reuse the template I want to be able to change the

ng-repeat='item in itemList'

But I have no idea how to append it to right element.

2
  • you dont need to do anything just change value of itemList ? Commented Jul 9, 2015 at 11:31
  • Yes, i want to be able to change to another list to fill my table with different types of items Commented Jul 9, 2015 at 11:34

3 Answers 3

1

Here is the simple explaination with your code./

Your html template -

<table class='table'>
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat='item in changableItemList'>
            <td>{{item.name}}</td>
            <td>{{item.description}}</td>
        </tr>
    </tbody>
</table>

Directive-With an isolate Scope

app.directive('kcItemTable', function() {
    return {
        restrict: 'E',
        scope :{
           itemList :'='
           },
        templateUrl: 'scripts/controllers/itemTableTemplate.html'
    }
})

You can use directive with different list --

     <div id='unannounced' kc-item-table item-list='ItemList1'>
        </div>
 <div id='unannounced' kc-item-table item-list='ItemList2'>
        </div>
Sign up to request clarification or add additional context in comments.

Comments

1

What you are trying to do is a very basic feature of AngularJS: data-binding to directives.

Check out the documentation about directives: https://docs.angularjs.org/guide/directive

Here is a very basic example forked from the above docs:

Main template:

  <div my-customer name="naomi"></div>
  <div my-customer name="boby"></div>

Directive:

.directive('myCustomer', function() {
    return {
      scope: {
        name: "@"
      },
      template: 'Name: {{name}}'
    };
  });

http://plnkr.co/edit/r9tIzwxCFyEyAU3NX0G1?p=preview

To clarify, what you need in your case is a "scope" property on your directive. You will be able to pass the scope values through the DOM element attributes.

Comments

0

Thats easy just add this to your div where you add your attribute directive.

<div ng-controller="YourCustomController" id='unannounced' kc-item-table>
</div>

then in YourCustomController you would put a $scope property called. $scope.changableItemList;

Or if you want multiple of these directives on the same page you can work with an isolated scope and do :

 <div id='unannounced' kc-item-table customList='customList2'/>

 <div id='unannounced' kc-item-table customList='customList1'/>

and in your directive do:

//you will need a controller above this which has $scope.customList declared

app.directive('kcItemTable', function() {
    return {
        restrict: 'E',
        scope :{
           customList :'=' //isolated scope with customList passed in
           },
        templateUrl: 'scripts/controllers/itemTableTemplate.html'
    }
})

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.