javascript - 具有相同功能的多个angularJS指令如何

标签 javascript angularjs angularjs-directive

我有多个 angularJS 指令(使用 chart.js 的 Angular 版本之一)

现在我有几个函数需要在这些指令中使用。

什么是让我不再重复自己的好方法,所以从指令中删除代码并将其放在一个地方。因为该代码无论如何都是相同的。 我研究了作用域的继承,但还没有解决这个问题。

这是在多个指令中使用的代码:

        $scope.widgetData = false;
        $scope.graphData = false;
        $scope.graphSelectorIndex = 0;

        $scope.graphSelector = [
            { 'byPeriod' : 'Periode'},
            { 'byHour' : 'Uur' },
            { 'byDay' : 'Dag'}
        ];

        $scope.graphSelectorByText = function (text) {
            switch (text) {
                case ('byPeriod'):
                    $scope.selector = 'byPeriod'
                    $scope.graphData = $scope.allGraphData.byPeriod;
                    $scope.graphType = 'Line';
                    break;
                case ('byDay'):
                    $scope.selector = 'byDay'
                    $scope.graphData = $scope.allGraphData.byDay;
                    $scope.graphType = 'Line';
                    break;
                case ('byHour'):
                    $scope.selector = 'byHour'
                    $scope.graphData = $scope.allGraphData.byHour;
                    $scope.graphType = 'Bar';
                    break;
            }
        }

        $scope.graphSelectorByInt = function (int) {
            switch (int) {
                case (0):
                    $scope.selector = 'byPeriod';
                    $scope.graphData = $scope.allGraphData.byPeriod;
                    $scope.graphType = 'Line';
                    break;
                case (1):
                    $scope.selector = 'byDay';
                    $scope.graphData = $scope.allGraphData.byDay;
                    $scope.graphType = 'Line';
                    break;
                case (2):
                    $scope.selector = 'byHour';
                    $scope.graphData = $scope.allGraphData.byHour;
                    $scope.graphType = 'Bar'
                    break;
            }
        }

        $scope.graphSelectorPrev = function () {
            $scope.graphSelectorIndex--;
            if ($scope.graphSelectorIndex < 0) {
                $scope.graphSelectorIndex = $scope.graphSelector.length-1;
            }
            $scope.graphSelectorByInt($scope.graphSelectorIndex);
            console.log($scope.graphSelectorIndex);

        }

        $scope.graphSelectorNext = function () {
            $scope.graphSelectorIndex++;
            if ($scope.graphSelectorIndex >= $scope.graphSelector.length) {
                $scope.graphSelectorIndex = 0;
            }
            $scope.graphSelectorByInt($scope.graphSelectorIndex);
            console.log($scope.graphSelectorIndex);
        }

一些 html:

    <div class="controls">
        <span class="btn_arrow previous inactive" ng-click="graphSelectorPrev()">Vorige</span>
        <p>{+ selector +}</p>
        <span class="btn_arrow next" ng-click="graphSelectorNext()">Volgende</span>
    </div>

感谢任何可以提供帮助的人!

最佳答案

首先,在这种情况下,您可以通过这种方式制作一些 DRY:

HTML:

<div class="controlls"> <span class="btn_arrow previous inactive" ng-click="graphSelectorInc(-1)">Vorige</span>

    <p>{{ selector }}</p> <span class="btn_arrow next" ng-click="graphSelectorInc(+1)">Volgende</span>

</div>

JS:

//Warning this approach is so called not monomorh(polymorph): the parameter of function is not strictly typed, so it lacks browser optimizations but in this case its not so important because code won't run often
$scope.graphSelectorByAny = function (step) {
    switch (step) {
        case ('byPeriod'):
        case (0):
            $scope.selector = 'byPeriod'
            $scope.graphData = $scope.allGraphData.byPeriod;
            $scope.graphType = 'Line';
            break;
        case ('byDay'):
        case (1):
            $scope.selector = 'byDay'
            $scope.graphData = $scope.allGraphData.byDay;
            $scope.graphType = 'Line';
            break;
        case ('byHour'):
        case (2):
            $scope.selector = 'byHour'
            $scope.graphData = $scope.allGraphData.byHour;
            $scope.graphType = 'Bar';
            break;
    }
}

$scope.graphSelectorInc = function (inc) {
    $scope.graphSelectorIndex += inc;
    if ($scope.graphSelectorIndex < 0) {
        $scope.graphSelectorIndex = $scope.graphSelector.length - 1;
    } else if ($scope.graphSelectorIndex >= $scope.graphSelector.length) {
        $scope.graphSelectorIndex = 0;
    }
    $scope.graphSelectorByAny($scope.graphSelectorIndex);
    console.log($scope.graphSelectorIndex);

}

我做了一个 JSFiddle给你。

但是有一个单独的指令可能也很好,我不能写因为我不知道你到底想要什么数据在 Controller 中。您可以阅读有关指令和数据绑定(bind)的信息 from the official docs .

也可以考虑使用 services当您希望多个 Controller /指令执行相同的操作时。

希望对你有所帮助。

关于javascript - 具有相同功能的多个angularJS指令如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27599540/

相关文章:

javascript - AngularJS 指令 - 如何让事件仅触发一次,否则它将在多个元素上触发?

angularjs - 简单的 Angular $routeProvider 解析测试。这段代码有什么问题?

javascript - 无论您在页面上的什么位置,我如何制作一个保持在固定位置的 div?

javascript - 如何在pouchDB/couchDB中查询多个条件

Javascript 格式化表格单元格中的空值

angularjs - 如何使用 Angular 将&lt;input tag>的 'value'属性中的数据绑定(bind)到NET core MVC模型

javascript - 对包装 slickgrid 的 Angular Directive(指令)进行单元测试

javascript - 调整条形图中现有线条的大小

javascript - Angularjs 检索特定名称/对象

javascript - $scope 数据转换为日期格式