【发布时间】:2013-01-29 12:23:34
【问题描述】:
如何使用动态模板创建指令?
'use strict';
app.directive('ngFormField', function($compile) {
return {
transclude: true,
scope: {
label: '@'
},
template: '<label for="user_email">{{label}}</label>',
// append
replace: true,
// attribute restriction
restrict: 'E',
// linking method
link: function($scope, element, attrs) {
switch (attrs['type']) {
case "text":
// append input field to "template"
case "select":
// append select dropdown to "template"
}
}
}
});
<ng-form-field label="First Name" type="text"></ng-form-field>
这就是我现在所拥有的,它可以正确显示标签。但是,我不确定如何在模板中附加额外的 HTML。或者将 2 个模板合并为 1 个。
【问题讨论】:
标签: angularjs angularjs-directive