Skip to content

Instantly share code, notes, and snippets.

@redconfetti
Created February 26, 2015 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redconfetti/4bfc1d494a5c36ffab17 to your computer and use it in GitHub Desktop.
Save redconfetti/4bfc1d494a5c36ffab17 to your computer and use it in GitHub Desktop.
Creating a Template with Inputs via a Directive in AngularJS
(function(angular) {
'use strict';
angular.module('myapp.directives').directive('myCustomTemplate', function() {
return {
restrict: 'E',
templateUrl: 'shared/my_custom_template.html', // markup for template
scope: {
myList: '=', // Attribute used to pass an array of objects to render via the template: data-my-list="myList"
myConditional: '=', // Attribute used to specify some conditional state in template: data-my-conditional="false"
doSomething: '&', // Attribute used to link outer scope function to inner context of template: data-do-something="outerScopeMethod(listItem)"
}
};
});
})(window.angular);
<div class="my-custom-template-container" data-ng-if="myList.length > 0">
<table class="my-custom-template-table" role="grid">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="row in myList">
<td data-ng-bind="row.name"></td>
<td data-ng-bind="row.address"></td>
<td data-ng-bind="row.phone"></td>
<td>
<button data-ng-click="doSomething({listItem: row})">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<my-custom-template data-my-conditional="true" data-my-list="listForTable" data-do-something="markRowDisabled(row)"></my-custom-templates>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment