- สร้างไฟล์ config ของ angular
- ส่วนเรียกใช้งาน config ผ่านทาง controller
- ส่วนแยกแยะ env ของ config เพื่อ warp ข้อมูลให้ก่อนนำไปใช้งาน
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** config constant */ | |
(function() { | |
var configModule = angular.module('configModule', []); | |
configModule.constant('config', { | |
local: { | |
baseApi: 'http://localhost', | |
port: '8080', | |
register: '/register' | |
timeOut: 1800, | |
}, | |
staging: { | |
baseApi: 'http://staging.acb', | |
port: '8080', | |
register: '/register' | |
timeOut: 1800, | |
}, | |
production: { | |
baseApi: 'http://production.acb', | |
port: '8080', | |
register: '/register' | |
timeOut: 1800, | |
}, | |
}); | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** use config app simple */ | |
(function() { | |
var useConfigApp = angular.module('switchEnv', ['configModule']); | |
useConfigApp.Controllers('simpleController', ['serviceEnv', function(serviceEnv) { | |
var baseUrl = serviceEnv.env.baseApi; | |
var port = serviceEnv.env.port; | |
var register = serviceEnv.env.register; | |
var timeOut = serviceEnv.env.timeOut; | |
}]); | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** switch env detect */ | |
(function() { | |
var switchEnv = angular.module('switchEnv', ['useConfigApp']); | |
switchEnv.service('serviceEnv', ['config','$location', function(config,$location) { | |
this.env = function(host){ | |
var host = !angular.isUndefined(host) ? host : $location.host(); | |
switch(host) { | |
case 'local': | |
return config.local; | |
break; | |
case 'staging': | |
return config.staging; | |
break; | |
case 'production': | |
return config.production; | |
break; | |
default: | |
return config.production; | |
break; | |
} | |
} | |
}]); | |
}()); |
0 ความคิดเห็น:
แสดงความคิดเห็น