0

here i am trying to create my own directive. I wanted to create data table. i don't know is i am doing anything wrong.please help me to find js-fiddle code ;

<div ng-app='test'>
 <div ng-controller='ctrl'>
  {{name}}
  <div ibatable tb-head='header' model='model' columns='columns' ></div>
</div>
 </div>

(function(){
  var app=angular.module('test',[]);
 app.controller('ctrl',function($scope){
$scope.name='ajith';
$scope.header=['Name','Class'];
$scope.columns=['m.name','m.class'];
$scope.model=[{name:'ajith',class:'12'},{name:'ajith1',class:'122'}];
});
app.directive('ibatable',function(){
 return{
restrict:'A',
scope:{tbHead:'=',model:'=',columns:'='},
template:"<table><tr ><th ng-repeat='h in tbHead'>{{h}}</th></tr><tr ng-      repeat='m in model'><td ng-repeat='c in columns'>{{m.+{{c}}}}</td></tr></table>"
  //here i wnt to call m.name and m.class dynamically
};
});
})();
5
  • help me please... even is this was a stupid question Commented Jan 25, 2016 at 13:23
  • dont know what you re trying to do but in the fiddle replace {} with [] in $scope.header Commented Jan 25, 2016 at 13:26
  • Vanojx , sorry the correct fiddle is jsfiddle.net/ajithravindran/36ed11ay Commented Jan 25, 2016 at 13:30
  • iam trying to repeat td Commented Jan 25, 2016 at 13:32
  • I know it is not exactly what u are looking for , but u can do like this :) jsfiddle.net/36ed11ay/3 Commented Jan 25, 2016 at 13:49

2 Answers 2

1

Replace your template by below:

template: "<table><tr><th ng-repeat='h in tbHead'>{{h}}</th></tr><tr ng-repeat='m in model'><td ng-repeat='c in m'>{{c}}</td></tr> </table>"
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you're making the template a bit more complicated than it needs to be. Here's what I did:

(function(){
    angular.module('test',[])
    .controller('ctrl',function($scope){
        $scope.name='ajith';
        $scope.header=['Class','Name'];
        $scope.model=[{name:'ajith',class:'12'},{name:'ajith1',class:'122'}];
    })
    .directive('ibatable',function(){
        return{
            restrict:'A',
            scope: {
        tbHead:'=',
        model:'=',
        columns:'='
       },
            template:"<table><tr><th ng-repeat='h in tbHead'>{{h}}</th></tr><tr ng-repeat='m in model'><td ng-repeat='(key, value) in m'>{{m[key]}}</td></tr></table>"
        };
    });
})();

More specifically, the template can be rewritten as:

<table>
    <tr>
        <th ng-repeat='h in tbHead'>{{h}}</th>
    </tr>
    <tr ng-repeat='m in model'>
        <td ng-repeat='(key, value) in m'>{{m[key]}}</td>
    </tr>
</table>

Rather than try to reference values in your model by building up the name, you can just iterate over the model itself by using the (key, value) notation with ngRepeat. Check out the Iterating over object properties section of the official docs for more info.

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.