我正在为我的网络应用程序设置一个基本模板,只使用一个指令,该指令具有返回不同模板的多个方法。
到目前为止,我所做的与调用服务
的方式相同SERVICE
app.service('myService', function() {
let path = "app/api/";
return {
getStudentInfo: () => {
//Some Statement...
}
}
})
现在,以防万一我目前如何调用指令。但是好像不行
DIRECTIVE
app.directive('baseTemplate', function() {
let path = "app/templates/"; // my basetemplate path folder
// I want to call specific methods returning the template I need.
return {
getCategory: () => {
return {
restrict: 'A',
template: '<strong> HELLO WORLD! </strong>'
}
},
getTable: () => {
return: {
restrict: 'A',
template: '<table> SOME DATA! </table>'
}
}
}
})
这就是我在调用指令时所做的
HTML
<div base-template.getCategory>
//The Output should be <strong> HELLO WORLD! </strong>
</div>
<div base-template.getTable>
//The same as the above Out should <table> SOME DATA! </table>
</div>
最佳答案
可以这样做:
<div base-template="getCategory">
<!-- The Output should be --><strong> HELLO WORLD! </strong>
</div>
<div base-template="getTable">
<!-- The same as the above Out should --><table> SOME DATA! </table>
</div>
app.directive('baseTemplate', function() {
let path = "app/templates/"; // my basetemplate path folder
return {
restrict: 'A',
template: function(tElem, tAttrs) {
switch (tAttrs.baseTemplate) {
case: "getCategory":
return `<strong> HELLO WORLD! </strong>`;
case: "getTable":
return `<table> SOME DATA! </table>`;
default:
return `<div>No Template Selected</div>`;
}
}
}
})
template
属性接受两个参数 tElement
和 tAttrs
并返回字符串值的函数。
有关详细信息,请参阅
关于angularjs - 一个指令内的多个方法返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58761612/