angularjs - 一个指令内的多个方法返回

我正在为我的网络应用程序设置一个基本模板,只使用一个指令,该指令具有返回不同模板的多个方法

到目前为止,我所做的与调用服务

的方式相同

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 并返回字符串值的函数。

有关详细信息,请参阅

  • AngularJS Comprehensive Directive API Reference - template

https://stackoverflow.com/questions/58761612/

相关文章:

python - 如何在 Python 中移动文件?

python - Tensorflow:在 pb 模型中使用 tensorflow.contrib.

c# - 检测浏览器中是否弹出打开文件对话框

go - 我可以防止 amqp.Channel 因错误而关闭吗?

c# - 如何检测进程是否移动了鼠标光标而不是用户?

c# - 显示哪个分数与哪个标签相关的 ML.Net 错误

javascript - 如何仅禁用 chrome 中的 "debugger"关键字,该关键字由循环

amazon-web-services - AWS DMS - 错误 - "AWS Account

python-3.x - matplotlib 错误 : No module named matpl

reactjs - React 中的动态标题内容