使用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