从控制器设置/获取服务中的数据
set/get data in service from controllers
我只是想弄清楚其中一些是如何工作的,所以我想在视图中输入一个数字,然后让控制器在服务和切换视图中设置数据。然后,下一个视图的控制器应该从同一个服务获取该编号,并最终将其传递给服务器。
在控制器中的getter/setter上,我得到
TypeError: undefined is not a function
但我似乎不明白为什么。我以为我已经遵循了文件和其他答案的建议,但我一定遗漏了一些小东西,或者完全误解了一些概念。
我的服务
'use strict';
/* Services
*/
var equipService = angular.module('getEquipService', []);
equipService.factory('GetEquipment', ['$resource', function($resource) {
equipService.siteId =1147;
// these first two I thought I did right but...nope
this.getId = function() {
return equipService.siteId;
}
this.setId = function(siteId) {
equipService.siteId = siteId;
}
return {
list: function(id) {
return $resource('http://example.com/somelist.cfm', {},{
query: {method:'POST', params: {id:id}, isArray:true}
})}
}}]);
控制器
var peopleController = angular.module('peopleController', []);
peopleController.controller('LoginController', ['GetEquipment', '$scope', '$log',
function(GetEquipment, $scope, $log){
$scope.buttonText = "Clicked";
$scope.inputId = "";
$scope.showPeople = function() {
// here I thought I could set the number entered
GetEquipment.setId(this.inputId);
$log.log("Success!");
$scope.buttonText = "Get Equipment";
};
}]);
peopleController.controller("PeopleController", ['$scope','$rootScope', '$routeParams', 'GetEquipment', '$log',
function($scope, $rootScope, $routeParams, GetEquipment, $log) {
$scope.people = GetEquipment.list(1260);
$log.log("showEuip: ", $scope.people);
$log.log("getting id: ", GetEquipment.getId());
}]);
此处
GetEquipment.setId(this.inputId);
是我得到上面提到的错误的地方(几个地方之一)。
我的理解是,由于我使用我的服务作为每个控制器的依赖项,我应该能够以这种方式访问它的功能。不确定我是错误地定义了函数还是其他什么。
我当然想知道为什么我所做的不起作用,但如果有更好的方法来传递输入数据,我愿意倾听。
我认为您可能混淆了angular中的factory()
和service()
方法。以下是如何使用以下任一方法实现GetEquipment
服务:
使用module.service()
equipService.service('GetEquipment', ['$resource', function ($resource) {
equipService.siteId = 1147;
// these first two I thought I did right but...nope
this.getId = function () {
return equipService.siteId;
};
this.setId = function (siteId) {
equipService.siteId = siteId;
};
this.list = function (id) {
return $resource('http://example.com/somelist.cfm', {}, {
query: {method: 'POST', params: {id: id}, isArray: true}
})
};
}]);
使用module.factory()
equipService.factory('GetEquipment', ['$resource', function ($resource) {
equipService.siteId = 1147;
return {
getId: function () {
return equipService.siteId;
},
setId: function (siteId) {
equipService.siteId = siteId;
},
list: function (id) {
return $resource('http://example.com/somelist.cfm', {}, {
query: {method: 'POST', params: {id: id}, isArray: true}
})
}
};
}]);
相关文章:
- AngularJS-需要在index.html页面中访问来自服务的数据
- 角度服务未返回数据
- 对视图上函数的角度绑定导致对数据服务的无限调用
- AngularJS中共享数据服务初始化失败
- Angular promise服务作为全局数据服务
- (IONIC)错误: [$injector:unpr] 未知提供程序: 数据服务提供程序 <- 数据服务<- 身份验证控
- 读取数据服务中的参数值
- 数据服务和离子的问题
- 过帐时更改oData条目中的属性.[WCF数据服务+EF+SQL expression]
- Angularjs无法将数据服务响应添加到作用域变量
- WSO2——在jaggery应用程序中调用数据服务
- Angularjs在控制器之间共享数据(服务方式)——不调用监视器
- 处理数据服务或控制器
- JS数据服务加载所有数据或只需要的数据
- 在AngularJS中使用Wcf数据服务
- 具有多个API数据服务的应用程序
- 将Breeze(使用Angular)连接到现有的WCF数据服务
- PhoneGap环境变量与开发环境中的AJAX数据服务有关
- Angular在控制器之间共享数据:服务与事件
- 从 WCF/ADO.NET 数据服务中的请求正文接收参数