[英]Alias the AngularJS $scope object in a controller
Without getting into the details of why, I need to provide an alias for $scope
in my controllers. 在不了解原因的细节的情况下,我需要在控制器中为$scope
提供别名。 Instead of injecting and decorating $scope
I'd like for the users to instead be able to inject view
and have it have the same effect. 我希望用户能够注入view
并使其具有相同的效果,而不是注入和装饰$scope
。
Based on my understanding of angular, $scope
is created by a $scopeProvider
which is a factory registered at configuration time of the angular app. 根据我对angular的理解, $scope
由$scopeProvider
创建,它是在angular app的配置时注册的工厂。 I'm assuming that I need to register a viewProvider
and set it equal to the $scopeProvider
but I haven't had luck with what I've been trying. 我假设我需要注册一个viewProvider
并将其设置为等于$scopeProvider
但我没有幸运,我一直在尝试。 Any ideas? 有任何想法吗?
FYI: I'm not looking for something like ['$scope', function(view){...
, the ideal solution would work with ['view', function(view){...
. 仅供参考:我不是在寻找类似['$scope', function(view){...
,理想的解决方案适用于['view', function(view){...
The view object would act exactly like $scope
, two way binding, etc. 视图对象的行为与$scope
,双向绑定等完全相同 。
If you are teaching AngularJS
, you should actually avoid using $scope
and use controller as syntax
-- See this awesome Angular Style Guide by John Papa for explanation . 如果您正在教授AngularJS
,您实际上应该避免使用$scope
并使用controller as syntax
- 请参阅John Papa撰写的这个令人敬畏的Angular Style Guide以获得解释 。
That would also solve your problem :) 那也可以解决你的问题:)
There is no such a thing as $scopeProvider
, scopes are created using the $new
method, every scope has access to this method through inheritance (delegation to be precise). 没有$scopeProvider
这样的东西,使用$new
方法创建范围,每个范围都可以通过继承访问此方法(准确地委托)。 When you ask for a $scope
in a controller, the $scope
is created dynamically using it's parent $scope
$new
method and injected under that name. 当您在控制器中请求$scope
时, $scope
是使用它的父$scope
$new
方法动态创建的,并以该名称注入。 To invoke the aforementioned behavior for a different name you would have to play with angular internals. 要为不同的名称调用上述行为,您必须使用角度内部。
I was able to solve it in my app's config section. 我能够在我的应用程序的配置部分解决它。 In my controllers now I can use $scope
or view
and they'll have the same meaning. 在我的控制器中,我现在可以使用$scope
或view
,它们具有相同的含义。
$routeProvider
.when('/Index', {
templateUrl: "/views/index.html",
controller: ['$scope', '$controller', function($scope, $controller){
$controller("IndexController", {$scope:$scope, view: $scope});
}]
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.