什么是延长AngularJS控制器推荐的方式? [英] What's the recommended way to extend AngularJS controllers?

查看:110
本文介绍了什么是延长AngularJS控制器推荐的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个控制器是非常相似的。我想有一个控制器,这三个延长并分享它的功能。


解决方案

也许的的不会延长控制器,但可以扩展控制器或使单个控制器多个控制器的一个mixin

  module.controller('CtrlImplAdvanced',['$范围,$控制器,函数($范围,$控制器){
    //初始化超类并扩展它。
    angular.extend(这一点,$控制器('CtrlImpl',{$范围:$范围}));
    ...附加扩展创建一个mixin。
}]);

当父控制器创建包含内也执行它的逻辑。
见$控制器(),以获得有关详细信息,但需要通过仅 $范围值。所有其他值将正常注入。

@mwarren 的,你关注的是由角依赖注入的照顾自动神奇。所有你需要的是注入$范围,但如果需要,您可以覆盖其他的注入值。
看看下面的例子:

\r
\r

(函数(角){\r
\r
变种模块= angular.module('stackoverflow.example',[]);\r
\r
module.controller('simpleController',函数($范围,$文件){\r
this.getOrigin =功能(){\r
返回$文件[0] .location.origin;\r
};\r
});\r
\r
module.controller('complexController',函数($范围,$控制器){\r
angular.extend(这一点,$控制器('simpleController',{$范围:$范围}));\r
});\r
\r
})(角);

\r

&LT;脚本SRC =htt​​ps://cdnjs.cloudflare.com/ajax /libs/angular.js/1.3.15/angular.js\"></script>\r
\r
&LT; D​​IV NG-应用=stackoverflow.example&GT;\r
    &LT; D​​IV NG控制器=complexController为C&GT;\r
        &LT;跨度&GT;&LT; B&GT;产地来自控制器:&LT; / B&GT; {{C.getOrigin()}}&下; /跨度&GT;\r
    &LT; / DIV&GT;\r
&LT; / DIV&GT;

\r

\r
\r

虽然$文档不传递到'simpleController'时,它是由complexController'$文件被注入我们。创建

I have three controllers that are quite similar. I want to have a controller which these three extend and share its functions.

解决方案

Perhaps you don't extend a controller but it is possible to extend a controller or make a single controller a mixin of multiple controllers.

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.
}]);

When the parent controller is created the logic contained within it is also executed. See $controller() for for more information about but only the $scope value needs to be passed. All other values will be injected normally.

@mwarren, your concern is taken care of auto-magically by Angular dependency injection. All you need is to inject $scope, although you could override the other injected values if desired. Take the following example:

(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);

<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>

Although $document is not passed into 'simpleController' when it is created by 'complexController' $document is injected for us.

这篇关于什么是延长AngularJS控制器推荐的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆