是的,正如您在第二个选项中描述的那样,我已经这样做了。
我创建了一个指令,该指令加载特定模板,然后根据数据的类型属性在其中包含其他指令。
directive("dynamicFormInput", ['$http', '$templateCache', function($http, $templateCache){
return {
restrict: 'E',
//currently need model for map center otherwise can be removed, need to set default in map directive
scope: {model: '=', section: '='},
template: '<ng:include src="tpl"></ng:include>',
link: function(scope, iElement, iAttrs) {
var sectionToLoad = "";
switch(scope.section.sectionTypeId)
{
case 1:
sectionToLoad ='partials/survey/textInput.html';
break;
case 2:
sectionToLoad = 'partials/survey/selectOneOption.html';
break;
case 3:
sectionToLoad = 'partials/survey/multiSelectOption.html';
break;
case 4:
sectionToLoad = 'partials/survey/boolean.html';
break;
case 5:
sectionToLoad = 'partials/survey/textInput.html';
break;
case 6:
if(scope.section.sectionId == 13)
sectionToLoad = 'partials/survey/addressSelection.html';
else if(scope.section.sectionId == 24)
sectionToLoad = 'partials/survey/contactForm.html'
break;
}
if(sectionToLoad!="")
{
$http.get(sectionToLoad, {cache:$templateCache});
scope.tpl=sectionToLoad;
}
}
}
}])
用法如下:
<accordion
close-others="true">
<accordion-group
ng-repeat="section in sections"
ng-class="{'isGroundTruthed':section.userId==73}"
heading="{{section.sectionName}} ({{displaySelection(section)}})">
<dynamic-form-input
section="section">
</dynamic-form-input>
</accordion-group>
</accordion>
你可以忽略手风琴,我只是碰巧用它作为我的重复项目,所以每个部分都会折叠起来。
编辑
刚刚清理了我的指令代码一些。