相关疑难解决方法(0)

AngularJS控制器可以从同一模块中的另一个控制器继承吗?

在模块中,控制器可以从外部控制器继承属性:

var app = angular.module('angularjs-starter', []);

var ParentCtrl = function ($scope, $location) {
};

app.controller('ChildCtrl', function($scope, $injector) {
  $injector.invoke(ParentCtrl, this, {$scope: $scope});
});
Run Code Online (Sandbox Code Playgroud)

示例:死链接:http://blog.omkarpatil.com/2013/02/controller-inheritance-in-angularjs.html

模块内的控制器也可以继承兄弟姐妹吗?

var app = angular.module('angularjs-starter', []);

app.controller('ParentCtrl ', function($scope) {
  //I'm the sibling, but want to act as parent
});

app.controller('ChildCtrl', function($scope, $injector) {
  $injector.invoke(ParentCtrl, this, {$scope: $scope}); //This does not work
});
Run Code Online (Sandbox Code Playgroud)

第二个代码不起作用,因为$injector.invoke需要一个函数作为第一个参数,并且找不到引用ParentCtrl.

inheritance angularjs angularjs-controller

197
推荐指数
6
解决办法
9万
查看次数

使用指令以角度分割控制器逻辑是一种好习惯吗?

我有非常复杂的控制器(约3K行代码)演示仪表板.Controller包含许多图表,网格表等.

例如,我将网格表逻辑移动到下面提到的指令名称wmGridActionItems.注意,它使用父范围:

app.directive('wmGridActionItems', ['$rootScope', '$timeout', function ($rootScope, $timeout) {
        return {
            restrict: 'E',
            templateUrl: 'views/grid-action-items.html',

            link: function (scope, elem, attrs) {

                // logic goes here
         }
        };
    }]);
Run Code Online (Sandbox Code Playgroud)

和HTML:

<div  ui-grid="gridActionItemsOptions"                           
                      ui-grid-auto-resize
                      ui-grid-pagination
                      ui-grid-selection
                      ui-grid-auto-resize
                      ui-grid-resize-columns>
                </div>
Run Code Online (Sandbox Code Playgroud)

所以在主控制器HTML中我只写:<wm-grid-action-items></wm-grid-action-items>

我无法在其他地方使用此指令,但至少我将我的BIG控制器划分为几个小指令,这些指令可以帮助我处理仪表板.

我做错了什么?这是好习惯吗?Angular有其他方法可以解决这个问题吗?

编辑

这是我$StateProvider的仪表板视图:

$stateProvider  
 .state('sidemenu.dash', {
                    url: '/dshmngr',
                    abstract: true,
                    views: {
                        'content': {
                            templateUrl: 'views/dashboard/dashboard_manager.html',
                            controller: 'DashboardMngrCtrl'
                        }
                    }


                })

                .state('sidemenu.dash.main', {
                    url: '/main',
                    views: {
                        'content': {
                            templateUrl: 'views/dashboard/dashboard-main.html',
                            controller: 'DashboardNewCtrl'
                        } …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive

6
推荐指数
1
解决办法
841
查看次数