0

假设我有一个图表指令(如饼图)和一个设置指令,其中包含用于修改图表指令设置的表单。设置指令显示在模式对话框中。

如何使设置指令中的更改在图表指令中生效?

可以有多个图表指令,每个都有独立的设置。每个图表的设置实际上将在设置模式中显示为选项卡,但我将其省略以保持示例简单。

以下是我尝试/考虑过的一些事情:

“使用工厂共享设置值。” 这失败了,因为我可能有多个图表指令,每个都有单独的设置。

“让设置指令成为图表指令的子项。” 设置指令不能是图表指令的子指令,因为它需要在模式中呈现。

这是一个带有一些相关片段的plunkr:

<div ng-controller="Controller">
    <a href ng-click="showSettings()">Settings</a>
    <chart></chart>
    <chart></chart>
</div>

JS:

app.controller("Controller", function ($scope, $modal) {
    $scope.showSettings = function () {
        $modal.open({
                    scope: $scope,
                    template: "<h1>Settings Modal</h1><settings></settings>"
                }
        );
    }
});

app.directive("chart", function () {
    return {
        restrict: 'E',
        template: '<h1>Chart Directive</h1><p>someChartSetting: {{someChartSetting}}</p>',
        controller: function ChartController($scope) {
            // This value represents a setting that I need to modify via the settings modal.
            $scope.someChartSetting = "on";
        }
    }
})

app.directive("settings", function () {
    return {
        restrict: 'E',
        template: '<a href ng-click="toggleSettings()">Toggle someChartSetting</a>',
        controller: function SettingsController($scope) {
            $scope.toggleSettings = function () {
                console.log("Toggle clicked");
                // This is where I need to modify the someChartSetting value from chart.
            }
        }
    }
})
4

1 回答 1

0

试试这个我希望它有帮助:

<!DOCTYPE html>
<head>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.11.2/ui-bootstrap-tpls.min.js"></script>
</head>

<body ng-app="app">
<div ng-controller="Controller">
    <a href ng-click="showSettings()">Settings</a>
    <chart></chart>
    <chart></chart>
</div>

<script>
    var app = angular.module("app", ['ui.bootstrap']);

    app.controller("Controller", function ($scope, $modal) {
        $scope.showSettings = function () {
            $modal.open({
                        scope: $scope,
                        template: "<h1>Settings Modal</h1><settings></settings>"
                    }
            );
        }
    });

    app.directive("chart", function () {
        return {
            restrict: 'E',
            template: '<h1>Chart Directive</h1><p>someChartSetting: {{someChartSetting}}</p>',
            controller: function ChartController($scope) {
                // This value represents a setting that I need to modify via the settings modal.
                $scope.someChartSetting = "on";
            }
        }
    })

    app.directive("settings", function () {
        return {
            restrict: 'E',
            template: '<a href ng-click="toggleSettings()">Toggle someChartSetting</a>',
            controller: function SettingsController($scope) {
                $scope.toggleSettings = function () {
                    // This is where I need to modify the someChartSetting value from chart.
                }
            }
        }
    })
</script>

</body>
</html>  

http://plnkr.co/edit/g0oGMzlsGYaSkumh4aho?p=preview

于 2014-10-30T14:06:17.877 回答