简体   繁体   English

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

[英]switch between 2 angulrjs tabs that using same controller

i am using angularjs-ui tabs in my app. 我在我的应用程序中使用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>'
    };


});

When I am using this directive to open 2 tabs that use different controllers it works well. 当我使用此指令打开使用不同控制器的2个选项卡时,它运行良好。 The problem occurs when I want to switch between 2 tabs that using same controller . 当我想在使用相同控制器的2个选项卡之间切换时,会出现问题 (for example, when I open 2 different projects and I want to switch between project number 1 and project number 2, the tab does not load all the data from project number 2 !!). (例如,当我打开2个不同的项目并且想要在项目编号1和项目编号2之间切换时,该选项卡不会加载项目编号2的所有数据!)。 I don't want to use angular-ui-router. 我不想使用angular-ui-router。 project 1 and project 2 uses same partial html 项目1和项目2使用相同的部分html

我设法通过使用Boradcasting消息解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM