2016-06-08 105 views
0
angular.module('starter.services', []) 
.controller('Friends', function() { 
console.log(global); 
}) 

angular.module('starter.anotherServices', []) 
.factory('Friends', function() { 
console.log(global); 
}) 

說我有很少的控制器,工廠等文件,我該如何創建一個全局變量,可以被所有人訪問?我必須創建一個.js文件,它將充當像db證書和http請求密鑰這樣的配置。在angularjs模塊中使用全局變量(config.js)

回答

0

您可以創建constant並注入任何你想訪問的地方。

var app = angular.module('yourModule', []); 
app.constant('Global', { 
    baseUtl: 'www.google.com' 
}); 

app.config(function(Global) { 
    console.log(Global); 
}); 
app.controller('YourController', function(Global) { 
    console.log(Global); 
}); 
0

您可以使用常數。

angular.module('starter.services', ['']) 
    .constant("global", { 
     "test": "1", 
    }) 

現在在所有控制器中注入此全局並使用它。

module.controller('Friends', function(global) { 
console.log(global); 
}) 
+0

我應該做global.test得到1正確嗎? –