I was just starting to use AngularJS and wanted to create a custom template directive to create in-place editable tables. The idea would be:
<tr ng-repeat="customer in model.customers">
<ng-template ng-hide="customer === model.selectedCustomer">
<td>{{customer.name}}</td>
</ng-template>
<ng-template ng-show="customer === model.selectedCustomer">
<td><input type="text" ng-model="customer.name"/></td>
</ng-template>
</tr>
Then it can also be extended to indicate the Url pattern, for example. <ng-template template-url="foo.html"></ng-template>
When I apply the directive ng-showto my custom directive, it does not work. Here is the code for my directive:
var demo = angular.module("demo", [])
.directive("ng-template", function() {
return {
restrict: "E",
replace: true,
transclude: true
}
});
and HTML ( http://jsfiddle.net/benfosterdev/ASXyy/ ):
<div ng-app="demo">
<table>
<tr ng-repeat="name in ['jane', 'john', 'frank']">
<ng-template ng-show="name !== 'frank'">
<td>{{name}}</td>
</ng-template>
</tr>
</table>
</div>
Also, when I look at the generated HTML, my custom directive doesn't even appear in the table:
<div ng-app="demo" class="ng-scope">
<ng-template ng-show="name !== 'frank'" class="">
</ng-template>
<table>
<tbody>
...
</tbody>
</table>
</div>
Essentially, I'm trying to avoid writing such code (setting a directive ng-showfor each element <td>):
<table>
<tr ng-repeat="customer in customers">
<ng-template>
<td ng-hide="isSelected">{{customer.name}}</td>
<td ng-hide="isSelected">{{customer.age}}</td>
<td ng-hide="isSelected"><button ng-click="edit(customer)"</td>
<td ng-show="isSelected"><input type="text" ng-model="customer.name"/></td>
<td ng-show="isSelected"><input type="text" ng-model="customer.age"/></td>
<td ng-show="isSelected"><button ng-click="save(customer)"</td>
</ng-template>
</tr>
</table>