在 AngularJS 中,是否可以创建私有(private) Controller 或服务,这些 Controller 或服务可以在定义它们的模块中使用,但不能由它们注入(inject)的另一个模块使用。
比如PrivateController是否可以设为子模块私有(private):
angular.module('Child', [])
.controller('PublicController', function ($scope){
$scope.children = ['Bob', 'Sue'];
})
.controller('PrivateController',function ($scope){
$scope.redHeadedStepChildren = ['Billy', 'Mildred'];
})
angular.module('Parent', ['Child'])
<div ng-app="Parent">
<div ng-controller='PublicController'>
<div ng-repeat='child in children'>
{{child}}
</div>
</div>
<div ng-controller='PrivateController'>
<div ng-repeat='child in redHeadedStepChildren'>
{{child}}
</div>
</div>
</div>
最佳答案
不,在当前版本的 AngularJS 中无法创建“私有(private)”服务。有一些关于支持私有(private)(模块范围)服务的讨论,但没有实现。
截至今天,在给定模块上公开的所有服务对所有其他模块都是可见的。
关于javascript - Angular 模块私有(private)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16950246/