我刚开始学习AngularJS。当我使用AngularJS的.service
时,如果我注入$RouteParams
,但我实际上没有使用$RouteParams
,那么.service
不起作用。
myApp.service('myService', function() {
this.name = 'myServiceName';
var self = this;
this.nameLength = function() {
return self.name.length;
};
});
myApp.controller('mainController', ['$scope','$log','$routeParams','myService',function($scope, $log,myService,$routeParams) {
$scope.handle = myService.name;
}]);
什么奇怪的是,如果我做的控制器使用$RouteParams
,那么它的工作原理,为什么会在$RouteParams
影响使用的.service
?
感谢Aditya,它的工作原理!这对我来说非常困惑,AngularJs实现了依赖注入。我会继续学习! –