AngularJS控制器可以从同一模块中的另一个控制器继承吗?
发布时间:2020-12-17 09:30:37 所属栏目:安全 来源:网络整理
导读:在一个模块中,控制器可以从外部控制器继承属性: var app = angular.module('angularjs-starter',[]);var ParentCtrl = function ($scope,$location) {};app.controller('ChildCtrl',function($scope,$injector) { $injector.invoke(ParentCtrl,this,{$scop
在一个模块中,控制器可以从外部控制器继承属性:
var app = angular.module('angularjs-starter',[]); var ParentCtrl = function ($scope,$location) { }; app.controller('ChildCtrl',function($scope,$injector) { $injector.invoke(ParentCtrl,this,{$scope: $scope}); }); 示例via:死链接: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',{$scope: $scope}); //This does not work }); 第二个代码不工作,因为$ injector.invoke需要一个函数作为第一个参数,没有找到ParentCtrl的引用。
是的,它可以,但你必须使用$控制器服务来实例化控制器,而不是: –
var app = angular.module('angularjs-starter',[]); app.controller('ParentCtrl',function($scope) { // I'm the sibling,$controller) { $controller('ParentCtrl',{$scope: $scope}); //This works }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |