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

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

컨트롤러, 공장 등에 대한 파일이 거의 없다고 말하면, 이들 모두가 액세스 할 수있는 전역 변수를 어떻게 만듭니 까? db 자격 증명 및 http 요청 키 같은 구성 역할을합니다 .js 파일을 만들어야합니다.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

나는 올바른 1을 얻으려면 global.test를 수행해야합니까? –

관련 문제