【发布时间】:2014-11-12 21:10:45
【问题描述】:
我创建了一个 angularjs 服务,我试图通过在控制器中注入服务来访问该服务。这是我使用的代码:
angular.module('ControllerModule').controller('AwarenessCntrl',['TestService',
function(TestService, $scope, $state)
{
console.log(TestService.list()[0].label);
$scope.goToHome = function()
{
$state.go('home');
};
}]);
我在控制台中收到以下错误消息: 错误:未定义不是对象(评估
'$scope.goToHome = function()
{
$state.go('home');
}')
这是我用于服务的代码:
angular.module('AppServices').service('TestService', [TestService]);
function TestService() {
var items = [{
id: 1,
label: 'Balle Balle, Service Chal Payi'
}, {
id: 2,
label: 'Item 1'
}];
this.list = function() {
return items;
};
this.add = function(item) {
items.push(item);
};
}
控制台能够打印来自服务的数据;但它也给出了我上面提到的错误消息。我到底做错了什么?
【问题讨论】: