一个指令中的多个方法 return

Multiple methods inside one Directive return

我正在为我的 Web 应用程序设置一个基本模板,仅使用 一个指令,它有 多个方法 返回不同的模板。

到目前为止我所做的与调用 Services

的方式相同

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 属性 一个带有两个参数的函数 tElementtAttrs 以及 returns 一个字符串值。

有关详细信息,请参阅