在使用相同控制器的两个angulrjs选项卡之间切换

switch between 2 angulrjs tabs that using same controller

本文关键字:angulrjs 两个 选项 之间 控制器      更新时间:2023-09-26

我在应用程序中使用angularjs ui选项卡。

    angular.module('bootstrap.tabset', [])
.directive('tekplntabsets',['tabsetServ', function (tabsetServ) {
    return {
        restrict: 'E',
        replace: true,
        transclude: true,
        controller: function ($scope) {
            $scope.templateUrl = '';
            var tabs = $scope.tabs = [];
            var controller = this;
            this.selectTab = function (tab) {
                angular.forEach(tabs, function (tab) {
                    tab.selected = false;
                });
                tab.selected = true;
                tabsetServ.setTabId(tab.tabid);
            };
            this.setTabTemplate = function(templateUrl) {
                $scope.templateUrl = templateUrl;
            };
            this.setTabController = function(tabCtrl) {
                $scope.tabCtrl = tabCtrl;
            };
            this.getTabController = function() {
                return $scope.tabCtrl;
            };
            this.removeTab = function(tab) {
                var index = tabsetServ.removeTab(tab);
                this.selectTab(tabs[index - 1]);
            };
            this.addTab = function (tab) {
                if (tabs.length == 0) {
                    controller.selectTab(tab);
                }
                tabs.push(tab);
                controller.selectTab(tab); 
            };
        },
        template:
          '<div class="row-fluid flexbox flexboxContent divHeight100">' +
            '<div class="row-fluid">' +
              '<div class="nav nav-tabs" ng-transclude></div>' +
            '</div>' +
            '<div class="row-fluid flexbox flexboxContent divHeight100">' +
              '<ng-include src="templateUrl" class="flexbox flexboxContent divHeight100" ></ng-include>' +
            '</div>' +
          '</div>'
    };
}])
.directive('tekplntab', function () {
    return {
        restrict: 'E',
        replace: true,
        require: '^tekplntabsets',
        scope: {
            title: '@',
            templateUrl: '@',
            tabid: '@',
            closable: '@',
            tabicon: '@',
            controller:'@'
        },
        link: function (scope, element, attrs, tabsetController,  $route) {
            tabsetController.addTab(scope);
            scope.select = function () {
                tabsetController.selectTab(scope);
            };
            scope.$watch('selected', function () {
                if (scope.selected) {
                    tabsetController.setTabTemplate('');
                    tabsetController.setTabTemplate(scope.templateUrl);
                   // scope.$apply();
                    //$route.reload();
                    //if (scope.$parent.tektab.ctrl !== "" && scope.$parent.tektab.ctrl !== "DashboardCtrl") {
                    //    var ctrl = scope.$parent.tektab.ctrl;
                    //    scope.$parent.tektab.ctrl = "";
                    //    scope.$parent.tektab.ctrl = ctrl;
                    //}
                    //if (scope.$root.$$phase !== '$digest' && scope.$$phase !== '$digest') {
                    //        scope.$apply();
                    //    }
                    //scope.$watch('scope.tabid', function (randomValue) {
                    //    scope.$apply();
                    //}); ng-controller="{{controller}}"
                }
            });
            scope.removeTab = function (id) {
              tabsetController.removeTab(id);
            };
        },
        template:
          '<li ng-class="{active: selected}" >' +
            '<a href="" ng-click="select()"><div  class="pointerDiv {{ tabicon }}" ng-show="{{closable}}"></div>&nbsp;{{ title }} ' +
              '<button  ng-click="removeTab(title)" class="TabClose" ng-show="{{closable}}">x</button></a>' +
            '<input type="hidden" value="{{ tabid }}"></input>' +
          '</li>'
    };

});

当我使用这个指令打开两个使用不同控制器的选项卡时,它工作得很好。当我想在使用同一控制器的两个选项卡之间切换时,就会出现问题。(例如,当我打开两个不同的项目,并且我想在项目编号1和项目编号2之间切换时,该选项卡不会加载项目编号2的所有数据!!)。我不想使用angular ui路由器。项目1和项目2使用相同的部分html

我使用Boradcasting消息

解决了这个问题