Несколько методов внутри одной директивы return - PullRequest
2 голосов
/ 08 ноября 2019

Я устанавливаю базовый шаблон для своего веб-приложения, используя только Одна директива , которая имеет Несколько методов , возвращая различные шаблоны.

То, что я сделал до сих пор, так этосделать это так же, как я звоню Службы

СЕРВИС

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>

1 Ответ

0 голосов
/ 08 ноября 2019

Это можно сделать следующим образом:

<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 и возвращает строковое значение.

Для получения дополнительной информации см.

...