在控制器中访问全局数据需要做什么?

时间:2016-03-15 11:11:02

标签: javascript angularjs ionic-framework

我有一个角度应用,定义如下,带有一些全局值:

angular.module('ionicApp', ['ionic', 'ngCordova', 'services'])
    .value('GlobalValues',
    {
        host : "http://localhost/",
        accountApi: 'MyService/api/AccountApi/'

        // ... other stuff like this
    })
    .run(function ($ionicPlatform) {
        // etc..
    })

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

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!
    };
})();

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

2 个答案:

答案 0 :(得分:0)

(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也不需要再次注入

答案 1 :(得分:0)

你搞砸了你的注射。注入语句中有一些值太多,或者方法签名中的值很少。

var welcome = "hello!"    
let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex
welcome.removeRange(range)
//I get "" as result rather than "hello" where the exclamation mark is removed

确保遵守进样器/功能签名顺序。