Javascript 访问服务中的多个变量/obj/funcs等

Javascript 访问服务中的多个变量/obj/funcs等,javascript,angularjs,Javascript,Angularjs,我对angularjs非常缺乏经验。我正在尝试实现一个跨控制器访问数据的服务。我举了个例子 下面是我的js。我没有发布我的HTML,因为我只是想先让js中的逻辑工作 angular.module('App') .factory('TestService', function() { return {testVar : '22222'} }) .controller('MainCtrl', function (TestService) {

我对angularjs非常缺乏经验。我正在尝试实现一个跨控制器访问数据的服务。我举了个例子

下面是我的js。我没有发布我的HTML,因为我只是想先让js中的逻辑工作

angular.module('App')
    .factory('TestService', function() {
        return {testVar : '22222'}
    })

    .controller('MainCtrl', function (TestService) {
        console.log(TestService.testVar);
    }
    .controller('SecCtrl', function (TestService) {
        console.log(TestService.testVar);
    }
这个很好用。每次访问两个控制器时,我都会记录控制台日志“22222”。我可以更新变量,并且在控制器之间可以很好地更新。如果我将
返回{testVar:'22222'}
替换为

return {
   testVar : { x : '22222' }
}
然后我将对象
x:'2222'
记录到控制台。这似乎也不错

问题是我不能真正理解控制台日志中
TestService.testVar
的用法。乍一看,我以为它是从服务TestService记录var testVar。然而,从这个示例来看,很明显,在本例中testVar实际上是一个键,返回的是键值

这是从服务返回某些内容(作为键值)的唯一方法吗

我希望使用服务访问服务中的多个值/对象等,但在示例中使用return只返回一个变量;除非我使用if语句,但这似乎有点过头了,好像我在使用控制器时真的遗漏了一些东西

有点像

angular.module('App')
    .factory('TestService', function() {
        (..code to choose what to return but the proper way...)
        return {testVar : '22222'}
        return {testVar2 : '333333'}
    })

    .controller('MainCtrl', function (TestService) {
        console.log(TestService.testVar);
        console.log(TestService.testVar2);
    }
    .controller('SecCtrl', function (TestService) {
        console.log(TestService.testVar);
        console.log(TestService.testVar2);
    }
我看了这个问题,但我认为它比我要问的更复杂。

第一次迭代 我们可以使用服务/工厂存储信息并在控制器中检索

angular.module('app', []).service('testService', function() {
    var testService = {};
    testService.x = 'x';
    testService.y = 'y';
    return testService;
}).controller('firstCtrl', function($scope, testService) {
    $scope.x = testService.x;
    $scope.y = testService.y;
}).controller('secondCtrl', function($scope, testService) {
    $scope.x = testService.x;
    $scope.y = testService.y;
});
angular.module('app', []).service('testService', function() {
    var testService = {
        someObject: {
            x: 'x',
            y: 'y',
            z: ''
        }
    };
    return testService;
}).controller('firstCtrl', function($scope, testService) {
    $scope.someObject = testService.someObject;
}).controller('secondCtrl', function($scope, testService) {
    $scope.someObject = testService.someObject;
});
jsfiddle:

第二次迭代 我们可以使用服务/工厂在控制器之间共享信息

angular.module('app', []).service('testService', function() {
    var testService = {};
    testService.x = 'x';
    testService.y = 'y';
    return testService;
}).controller('firstCtrl', function($scope, testService) {
    $scope.x = testService.x;
    $scope.y = testService.y;
}).controller('secondCtrl', function($scope, testService) {
    $scope.x = testService.x;
    $scope.y = testService.y;
});
angular.module('app', []).service('testService', function() {
    var testService = {
        someObject: {
            x: 'x',
            y: 'y',
            z: ''
        }
    };
    return testService;
}).controller('firstCtrl', function($scope, testService) {
    $scope.someObject = testService.someObject;
}).controller('secondCtrl', function($scope, testService) {
    $scope.someObject = testService.someObject;
});
请看一看,我正在复述object
someObject
,其余部分由Angular diggest制作


jsIDLE:

您可以尝试以下方法:

angular.module('App')
    .factory('TestService', function() {
        // Code to choose what to return but the proper way
        return {testVar2 : '333333', testVar : '22222'}
    })

别忘了分号,只需返回两个键:
testVar
testVar2
@krzysztof谢谢,这很有效。但是,如果我希望访问的服务中有许多变量,这是否意味着每次都必须返回所有密钥?似乎inefficient@myol,不,我将改进我的答案以显示其他可能性。我仍然不完全理解控制器的使用,但这给了我一个更坚实的工作基础。