Created
February 26, 2015 19:46
-
-
Save redconfetti/4bfc1d494a5c36ffab17 to your computer and use it in GitHub Desktop.
Creating a Template with Inputs via a Directive in AngularJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> </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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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