2015-08-28 60 views

回答

2

我想你可以让你的控制器从一个共同的基础控制器继承。像这样的东西可能会奏效:

angular.module('extending', []) 

.controller('baseController', function(someService) { 
    this.someService = someService; 
}) 

.controller('extendedController', function($scope, $controller) { 
    angular.extend(this, $controller('baseController', { $scope: $scope })); 

    this.alert = this.someService.alert; 
}) 

.service('someService', function() { 
    this.alert = function() { 
    window.alert('alert some service'); 
    }; 
}); 

HTML

<body> 
    <div ng-controller="extendedController as ex"> 
     <button ng-click="ex.alert()">Alert</button> 
    </div> 
    </body> 

例如在plunker。 SO上的Related帖子。 AngularjS extend doc。

+0

非常感谢!你的回答非常有帮助 –

+0

哇,不知道'延伸'功能,+1 –