在模块中,控制器可以从外部控制器继承属性: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模块内的控制器也可以继承兄弟姐妹吗?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。
3 回答
哔哔one
TA贡献1854条经验 获得超8个赞
是的,它可以但您必须使用该$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});
- 3 回答
- 0 关注
- 622 浏览
添加回答
举报
0/150
提交
取消