0

I'm trying to use angular directive with table, but I don't see any data, when I use list I can see the data and it works properly. I'm using bootstrap for styling the table too. I really don't know what I'm doing wrong,

Hope you can help, here are the main parts of my code:

main.html:

  <table class="table">
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Username</th>
        </tr>
    </thead>
    <tbody ng-repeat="m in users">

        <my-dir my-obj="m"></my-dir>

    </tbody>
</table>

app.js:

var myApp = angular.module('myApp', ['ngRoute'])

myApp.config(function($routeProvider) {

    $routeProvider
        .when('/', {
        templateUrl: 'pages/main.html',
        controller: 'AppCtrl'
    })
 

});

myApp.controller('AppCtrl', function($scope) {
    

    $scope.users = [{name: 'john', age: 16}, {name: 'dani', age: 10}];
    
    
    
});

myApp.directive('myDir', function(){
    return{
        templateUrl: 'directive/mytbl.html',
        replace: false,
        scope:{
            myObj: '='
        }
    }
})

mytbl.html:

<td>{{ myObj.name }}</td>
<td>{{ myObj.age }}</td>

1 Answer 1

1

You are creating a <tbody> for each user.

Try changing to this:

<tbody>
   <tr ng-repeat="m in users" my-dir my-obj="m"></tr>
</tbody>

The directive is essentially appending it's template to the element in which it is specified (creating children of that element). So in this case, the children are those of .

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

3 Comments

it works now, thank you! I didn't know that it possible to put the directive inside the element, what happen actually when doing this?
The directive is essentially appending it's template to the element in which it is specified (creating children of that element). So in this case, the children are those of <tr>.
So what is the difference between your first answer(before editing)? The directive is also a child of the <tr>

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.