在模塊中,控制器可以從外部控制器繼承屬性:var app = angular.module('angularjs-starter', []);var ParentCtrl = function ($scope, $location) {};app.controller('ChildCtrl', function($scope, $injector) {
$injector.invoke(ParentCtrl, this, {$scope: $scope});});示例:死鏈接:http://blog.omkarpatil.com/2013/02/controller-inheritance-in-angularjs.html模塊內(nèi)的控制器也可以繼承兄弟姐妹嗎?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需要一個函數(shù)作為第一個參數(shù),并且找不到引用ParentCtrl。
3 回答

嗶嗶one
TA貢獻1854條經(jīng)驗 獲得超8個贊
是的,它可以但您必須使用該$controller
服務(wù)來實例化控制器: -
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});
- 3 回答
- 0 關(guān)注
- 647 瀏覽
添加回答
舉報
0/150
提交
取消