我有類似的情況,我想從生產/開發環境中分離變量值。
而不是使用$ rootScope.configurableVar,你應該使用angular constants。首先用角度常量更新您的API網址引用。
然後您可以使用gulp-ng-config生成常量文件。
例如,假設你有一個config.json文件:
{
"local": {
"EnvironmentConfig": {
"api": "http://localhost/"
}
},
"production": {
"EnvironmentConfig": {
"api": "https://api.production.com/"
}
}
}
使用插件也很簡單:
gulpNgConfig('myApp.config', {
environment: 'production'
})
,這將產生以下代碼:
angular.module('myApp.config', [])
.constant('EnvironmentConfig', {"api": "https://api.production.com/"});
因此,要使用這與吞噬:
var gulp = require('gulp');
var gulpNgConfig = require('gulp-ng-config');
var rename = require('gulp-rename');
gulp.task('config:production', function(){
// this will generate a www/js/config.js file with production values
gulp.src('config.json')
.pipe(gulpNgConfig('myApp.config', { environment: 'production' }))
.pipe(rename('config.js'))
.pipe(gulp.dest('www/js'))
})
要生成角常數,在終端只需使用以下命令:
gulp config:production
而在你的應用程序angular.js只是使用常量:
angular.module('myApp.controllers').controller('myController', ['EnvironmentConfig', '$http', function(EnvironmentConfig, $http){
$http.get(EnvironmentConfig.url + '/path');
}])