我需要做什么才能访问控制器中的全局数据

What do i need to do to access global data in a controller?

本文关键字:控制器 访问控制 数据 全局 访问 什么      更新时间:2023-09-26

我有一个角度应用程序,定义如下,其中包含一些全局值:

angular.module('ionicApp', ['ionic', 'ngCordova', 'services'])
    .value('GlobalValues',
    {
        host : "http://localhost/",
        accountApi: 'MyService/api/AccountApi/'
        // ... other stuff like this
    })
    .run(function ($ionicPlatform) {
        // etc..
    })

我能够在我的UserService中访问GlobalValues,定义为:

angular.module('services', [])
    .service('UserService', function($q, $http, $ionicLoading, GlobalValues) {
        alert(GlobalValues.host); // has a value
    });

但在我的CreateAdController中,GlobalValues是未定义的:

(function() {
    'use strict';
    angular.module('ionicApp')
        .controller('CreateAdController', ['$cordovaCamera', 'Camera', '$scope', '$http', 'GlobalValues', CreateAdController]);
    function CreateAdController($cordovaCamera, $scope, $http, GlobalValues) {
        alert(GlobalValues.host); // is undefined!
    };
})();

我需要做什么才能从我的CreateAdController访问GlobalValues中的数据?

(function() {
'use strict';
angular.module('ionicApp')
    .controller('CreateAdController', ['$cordovaCamera', 'Camera', '$scope', '$http', 'GlobalValues']);
function CreateAdController($cordovaCamera, Camera, $scope, $http, GlobalValues) {
    alert(GlobalValues.host); // is undefined!
};
})();

以上应该有效,我做了一些更改。此外,CreateAdController 也不需要再次注入

你打

了针搞砸了。注入语句中的值太多,方法签名中的值很少。

(function() {
    'use strict';
    angular.module('ionicApp')
        .controller('CreateAdController', ['$cordovaCamera', 'Camera', '$scope', '$http', 'GlobalValues']);
    function CreateAdController($cordovaCamera, Camera,  $scope, $http, GlobalValues) {
        alert(GlobalValues.host); // is undefined!
    };
})();

确保遵守注入器/函数签名顺序。