Stop $ mdPanel控制器一旦打开就调用

问题描述 投票:0回答:1

在我的AngularJS应用中,我正在使用$mdPanel服务来提供过滤器面板。当用户单击按钮时,将调用该服务,并且当用户选择过滤器选项时,它将更新表,并在表的标题中提供过滤器芯片(用户可以看到他们选择的内容)。

view -table

<md-button data-ng-click="$ctrl.showFilter($event)">
     Filter
</md-button>

查看过滤器面板

<div>
    <md-list ng-repeat="cat in categories">

    </md-list>
    <div>
        <md-button data-ng-click="closeDialog();">
            Close
        </md-button>
    </div>
</div>

控制器

var panelRef;

function showFilter($event) {
    var config = {
        attachTo: angular.element(document.body),
        controller: ['mdPanelRef', '$scope', function(mdPanelRef, $scope) {

          //LOGIC TO FILTER TABLE DATA AND PROVIDE SELECTED FILTER OPTION GOES HERE

        }],
        controllerAs: '$ctrl',
        targetEvent: $event,
        templateUrl: 'app/templates/portfolio/filter-panel.tpl.htm',
        clickOutsideToClose: true,
        escapeToClose: true,
    };

    $mdPanel.open(config)
    .then(
        function(result) {
            panelRef = result;
        },
        function(error){

        }
    );
}

问题是,通过调用showFilter打开过滤器面板,然后关闭,然后再次重新打开广告,然后再次调用该服务,调用新实例并重置mdPanel配置控制器中保存的所有值,重置我不想要的过滤器选项。

问题

如何从初始打开状态维护mdPanel的单个实例?用户可以随意打开和关闭它,但是控制器中的所有值均已保存

javascript angularjs angularjs-material
1个回答
0
投票

您可以尝试将过滤后的数据存储在父控制器中。

(parent controller)
$scope.filterData = {};

然后使用showFilter($event)功能传递过滤器数据。

(view -table)
<md-button data-ng-click="$ctrl.showFilter($event, filterData)">
 Filter
</md-button>

然后尝试在配置文件中使用locals

(panel controller)
var panelRef;

function showFilter($event, filterData) {
    var config = {
        attachTo: angular.element(document.body),
        controller: ['mdPanelRef', '$scope', function(mdPanelRef, $scope) {

          //LOGIC TO FILTER TABLE DATA AND PROVIDE SELECTED FILTER OPTION GOES HERE

        }],
        controllerAs: '$ctrl',
        targetEvent: $event,
        templateUrl: 'app/templates/portfolio/filter-panel.tpl.htm',
        clickOutsideToClose: true,
        escapeToClose: true,
        locals: {
            filterData: filterData //add local
        }
    };

    $mdPanel.open(config)
    .then(
        function(result) {
            panelRef = result;
        },
        function(error){

        }
    );
}
© www.soinside.com 2019 - 2024. All rights reserved.