Angularjs 无法将服务注入Angular.js项目中的控制器
我试图创建一个简单的单页应用程序与角度。我正在尝试将自定义服务连接到主控制器。它只是将字符串的长度返回给控制器 这来自我的app.js:Angularjs 无法将服务注入Angular.js项目中的控制器,angularjs,Angularjs,我试图创建一个简单的单页应用程序与角度。我正在尝试将自定义服务连接到主控制器。它只是将字符串的长度返回给控制器 这来自我的app.js: angular .module('weatherApp', [ 'ngResource', 'ngRoute' ]) .config(function ($routeProvider) { $routeProvider .when('/', { templateUrl: 'views/main.html', co
angular
.module('weatherApp', [
'ngResource',
'ngRoute'
])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'main'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutCtrl',
controllerAs: 'about'
})
.otherwise({
redirectTo: '/'
});
});
这是我的主控制器:
angular.module('weatherApp')
.controller('MainCtrl', ['$scope', 'weatherService', function ($scope, weatherService) {
$scope.name = weatherService.name;
$scope.$watch('name', function() {
weatherService.name = $scope.name;
});
$log.log(weatherService.name);
$log.log(weatherService.namelength());
}]);
这就是服务:
angular.module('weatherApp')
.service('weatherService', function () {
var self = this
this.name = 'Moscow weather';
this.namelength = function() {
return self.name.length;
};
});
浏览器调试器中出现错误:
Error: [$injector:unpr] Unknown provider: weatherServiceProvider <- weatherService <- MainCtrl
你可能忘了在服务的HTML页面中添加标签。谢谢你,我太傻了。