使用Angularjs 1.x,您可以在编辑/只读模式之间点击按钮轻松切换html模板。 ng-include指令是关键。在用户角度动态切换html模板2
<table>
<thead>
<th>Name</th>
<th>Age</th>
<th></th>
</thead>
<tbody>
<tr ng-repeat="contact in model.contacts" ng-include="getTemplate(contact)">
</tr>
</tbody>
</table>
的get是getTemplate函数执行此代码:
$scope.getTemplate = function (contact) {
if (contact.id === $scope.model.selected.id) return 'edit';
else return 'display';
};
这又导致这些模板之一,活跃在UI:
显示
<script type="text/ng-template" id="display">
<td>{{contact.name}}</td>
<td>{{contact.age}}</td>
<td>
<button ng-click="editContact(contact)">Edit</button>
</td>
</script>
编辑
<script type="text/ng-template" id="edit">
<td><input type="text" ng-model="model.selected.name" /></td>
<td><input type="text" ng-model="model.selected.age" /></td>
<td>
<button ng-click="saveContact($index)">Save</button>
<button ng-click="reset()">Cancel</button>
</td>
</script>
https://jsfiddle.net/benfosterdev/UWLFJ/
具有角2 RC 4存在无正包括。
我该如何对Angular 2 RC4做同样的功能?
令人敬畏的演示!代码非常类似于角1.X!你能告诉我是什么声明:@ViewChild('editTmpl')editTmpl:TemplateRef,我打字打字几个星期了,我从来没有见过这种语法。 –
Pascal
这只是访问模板里面的组件类https://angular.io/docs/ts/latest/api/core/index/TemplateRef-class.html – yurzui
好的谢谢。我更喜欢你的解决方案,远远超过dfsq在RC2之前的角度2和你的角度为RC2 +的实现方法:-) – Pascal