Create a reusable list in Angularjs -
i rather new angularjs , trying create reusable list of kind of entity (e.g.: todo, contacts, , on). each entity type should have different kind of presentation. furthermore, list should sortable attributes defined in dedicated entity controller.
i able achieve following markup:
<entitylist> <entitylist:orderby> <option value="created">created</option> <option value="name">name</option> </entitylist:orderby> <entitylist:items> <entitylist:item ng-repeat="entity in entities | orderby:orderby" ng-click="showdetails(entity)"> <input type="checkbox" ng-model="entity.completed"/> {{ entity.title }} </entitylist:item> </entitylist:items> </entitylist> but seemed redundant use template each entity type. tried (entity template):
<entitylist sortable="sortable" entities="todos"> <input type="checkbox" ng-model="entity.completed"/> {{ entity.title }} </entitylist> with following directive:
angular.module('app.components') .directive('entitylist', function() { return { restrict: 'e', controller: 'entitylistcontroller', replace: true, templateurl: 'components/entitylist/entitylist.tpl.html', transclude: true, scope: { entities: '=' } }; }) .controller('entitylistcontroller', ['$scope', '$rootscope', function($scope, $rootscope) { $scope.orderby = $scope.$parent.orderby; $scope.sortable = $scope.$parent.sortable; }]) ; the controller in ui-router state):
controller: ['$scope', '$state', 'entities', function( $scope, $state, entities) { $scope.todos = entities; $scope.orderby = 'name'; $scope.sortable = [{value: 'name', name: 'name'}, {value: 'created', name: 'created'}]; } ] and directive template (components/entitylist/entitylist.tpl.html):
<div class="entitylist"> <div class="entitylist-nav"> <select ng-model="orderby" class="form-control"> <option ng-repeat="sort in sortable" value="sort.value">{{ sort.name }}</option> </select> </div> <ul class="entity-list"> <li ng-repeat="entity in entities | orderby:orderby"> <div ng-transclude=""></div> </li> </ul> </div> but here struggling hours isolated scopes of ng-repeat , ng-transclude , not able make work. created jsfiddle.
my question is: on right track? what's angular way of achieving this? i've been googling days find relevant, had no success. there examples of (apart single list tutorials)?
Comments
Post a Comment