从控制器设置/获取服务中的数据

set/get data in service from controllers

本文关键字:数据 服务 获取 控制器 设置      更新时间:2023-10-03

我只是想弄清楚其中一些是如何工作的,所以我想在视图中输入一个数字,然后让控制器在服务和切换视图中设置数据。然后,下一个视图的控制器应该从同一个服务获取该编号,并最终将其传递给服务器。

在控制器中的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}
            })
        }
    };
}]);