AngularJS控制器能否继承同一模块中的另一个控制器?
在模块中,控制器可以继承外部控制器的属性:
var app = angular.module('angularjs-starter', []);
var ParentCtrl = function ($scope, $location) {
};
app.controller('ChildCtrl', function($scope, $injector) {
  $injector.invoke(ParentCtrl, this, {$scope: $scope});
});
示例via: Dead link :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
});
  第二个代码不起作用,因为$injector.invoke需要一个函数作为第一个参数,并且没有找到对ParentCtrl的引用。 
是的,它可以,但你必须使用$controller服务实例化控制器: - 
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, $controller) {
  $controller('ParentCtrl', {$scope: $scope}); //This works
});
  如果你使用vm控制器语法,这里是我的解决方案: 
.controller("BaseGenericCtrl", function ($scope) {
    var vm = this;
    vm.reload = reload;
    vm.items = [];
    function reload() {
        // this function will come from child controller scope - RESTDataService.getItemsA
        this.getItems();
    }
})
.controller("ChildCtrl", function ($scope, $controller, RESTDataService) {
    var vm = this;
    vm.getItems = RESTDataService.getItemsA;
    angular.extend(vm, $controller('BaseGenericCtrl', {$scope: $scope}));
})
  不幸的是,您不能使用$controller.call(vm, 'BaseGenericCtrl'...)将当前上下文传递给closure(用于reload() )函数,因此只有一种解决方案是使用this内部继承函数动态更改上下文。 
我认为,您应该使用工厂或服务,为两个控制器提供可访问的功能或数据。
这里是类似的问题---> AngularJS控制器的继承
链接地址: http://www.djcxy.com/p/77853.html上一篇: Can an AngularJS controller inherit from another controller in the same module?
