Enz*_*zey 301
也许您没有扩展控制器,但可以扩展控制器或使单个控制器成为多个控制器的混合.
module.controller('CtrlImplAdvanced', ['$scope', '$controller', function ($scope, $controller) {
// Initialize the super class and extend it.
angular.extend(this, $controller('CtrlImpl', {$scope: $scope}));
… Additional extensions to create a mixin.
}]);
Run Code Online (Sandbox Code Playgroud)
创建父控制器时,还会执行其中包含的逻辑.有关更多信息,请参阅$ controller(),但只$scope
需要传递值.所有其他值将正常注入.
@mwarren,您的关注点由Angular依赖注入自动神奇地处理.您需要的只是注入$ scope,尽管您可以根据需要覆盖其他注入的值.请看以下示例:
(function(angular) {
var module = angular.module('stackoverflow.example',[]);
module.controller('simpleController', function($scope, $document) {
this.getOrigin = function() {
return $document[0].location.origin;
};
});
module.controller('complexController', function($scope, $controller) {
angular.extend(this, $controller('simpleController', {$scope: $scope}));
});
})(angular);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js"></script>
<div ng-app="stackoverflow.example">
<div ng-controller="complexController as C">
<span><b>Origin from Controller:</b> {{C.getOrigin()}}</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
虽然在'complexController'创建$ document时,$ document没有传递给'simpleController',但是我们注入了$ document.
Min*_*hev 52
对于继承,您可以使用标准JavaScript继承模式.这是一个使用的演示$injector
function Parent($scope) {
$scope.name = 'Human';
$scope.clickParent = function() {
$scope.name = 'Clicked from base controller';
}
}
function Child($scope, $injector) {
$injector.invoke(Parent, this, {$scope: $scope});
$scope.name = 'Human Child';
$scope.clickChild = function(){
$scope.clickParent();
}
}
Child.prototype = Object.create(Parent.prototype);
Run Code Online (Sandbox Code Playgroud)
如果您使用controllerAs
语法(我强烈推荐),使用经典继承模式会更容易:
function BaseCtrl() {
this.name = 'foobar';
}
BaseCtrl.prototype.parentMethod = function () {
//body
};
function ChildCtrl() {
BaseCtrl.call(this);
this.name = 'baz';
}
ChildCtrl.prototype = Object.create(BaseCtrl.prototype);
ChildCtrl.prototype.childMethod = function () {
this.parentMethod();
//body
};
app.controller('BaseCtrl', BaseCtrl);
app.controller('ChildCtrl', ChildCtrl);
Run Code Online (Sandbox Code Playgroud)
另一种方法是创建只是"抽象"构造函数,它将是你的基本控制器:
function BaseController() {
this.click = function () {
//some actions here
};
}
module.controller('ChildCtrl', ['$scope', function ($scope) {
BaseController.call($scope);
$scope.anotherClick = function () {
//other actions
};
}]);
Run Code Online (Sandbox Code Playgroud)
And*_*ves 16
好吧,我不确定你想要实现什么,但通常服务是可行的.您还可以使用Angular的Scope继承特性在控制器之间共享代码:
<body ng-controller="ParentCtrl">
<div ng-controller="FirstChildCtrl"></div>
<div ng-controller="SecondChildCtrl"></div>
</body>
function ParentCtrl($scope) {
$scope.fx = function() {
alert("Hello World");
});
}
function FirstChildCtrl($scope) {
// $scope.fx() is available here
}
function SecondChildCtrl($scope) {
// $scope.fx() is available here
}
Run Code Online (Sandbox Code Playgroud)
Bar*_*art 15
您不扩展控制器.如果它们执行相同的基本功能,则需要将这些功能移动到服务中.该服务可以注入您的控制器.
Nik*_*rov 10
然而,从该采取的另一项很好的解决方案的文章:
// base controller containing common functions for add/edit controllers
module.controller('Diary.BaseAddEditController', function ($scope, SomeService) {
$scope.diaryEntry = {};
$scope.saveDiaryEntry = function () {
SomeService.SaveDiaryEntry($scope.diaryEntry);
};
// add any other shared functionality here.
}])
module.controller('Diary.AddDiaryController', function ($scope, $controller) {
// instantiate base controller
$controller('Diary.BaseAddEditController', { $scope: $scope });
}])
module.controller('Diary.EditDiaryController', function ($scope, $routeParams, DiaryService, $controller) {
// instantiate base controller
$controller('Diary.BaseAddEditController', { $scope: $scope });
DiaryService.GetDiaryEntry($routeParams.id).success(function (data) {
$scope.diaryEntry = data;
});
}]);
Run Code Online (Sandbox Code Playgroud)
您可以通过注入来创建服务并在任何控制器中继承其行为.
app.service("reusableCode", function() {
var reusableCode = {};
reusableCode.commonMethod = function() {
alert('Hello, World!');
};
return reusableCode;
});
Run Code Online (Sandbox Code Playgroud)
然后在您想要从上面的reusableCode服务扩展的控制器中:
app.controller('MainCtrl', function($scope, reusableCode) {
angular.extend($scope, reusableCode);
// now you can access all the properties of reusableCode in this $scope
$scope.commonMethod()
});
Run Code Online (Sandbox Code Playgroud)
DEMO PLUNKER:http://plnkr.co/edit/EQtj6I0X08xprE8D0n5b?p=preview
您可以尝试这样的事情(尚未测试):
function baseController(callback){
return function($scope){
$scope.baseMethod = function(){
console.log('base method');
}
callback.apply(this, arguments);
}
}
app.controller('childController', baseController(function(){
}));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
78586 次 |
最近记录: |