Gulp - 从AngularJS服务访问gulpfile变量

时间:2017-01-10 11:43:50

标签: angularjs node.js web-services gulp

我有一个服务,我在那里管理我对webservice的请求,实际上我已经在这个服务上定义了webservice url:

(function ()
{
    angular.module("MyProject")

    .factory("Webservice", ["$http", function ($http)
    {
        var URLbase = function ()
        {
            //return "http://myproject.link.pt/Service/Service.svc/";
            return "http://localhost/MyProject/Service.svc/";
        }

        var login = function (param) {
            var serviceURL = URLbase() + "ValidateLogin";
            return $http.post(serviceURL, JSON.stringify({ username: param.Username, password: param.Password }));
          }
        var loginResult = function (response) {
            return exists(response) && exists(response.data) && exists(response.data.ValidateLoginResult)
                ? response.data.ValidateLoginResult
                : null;
        }

        return {
            Login: login,
            LoginResult: loginResult
        }
    }])
})();

但是当我需要发布我的项目时,我经常忘记评论并取消注释URLbase()上的路径。

有什么方法可以在gulpfile.js

上执行此操作

在我的gulpfile.js上,我有两个不同的任务要构建用于开发或生产,我认为如果我可以在那里进行管理那就很好:

gulp.task('build-development', function (done)
{
    config.minify = false;

    var tasks = ['html', 'resources', 'assets', 'sass', 'js', 'vendors'];
    seq('clean', tasks, done);
});

gulp.task('build-production', function (done)
{
    config.minify = true;

    var tasks = ['html', 'resources', 'assets', 'sass', 'js', 'vendors'];
    seq('clean', tasks, function() {
        notifier.notify({ title: 'Project publishment', message: 'Done' });
    });
});

我已经尝试在我的gulpfile.js上创建一个变量并在AngularJS文件上访问它但我收到一条错误,说明该变量未定义。

1 个答案:

答案 0 :(得分:1)

在网络服务中使用占位符:

obsDates =  c("2014", "2015")

migrations = data.frame(obsDate = c("2014", "2015", "2015", "2014", "2015"),
                        t1 = c("A", "A", "B", "C", "C"),
                        t2 = c("A", "B", "C", "A", "B"),
                        number = c(1, 8, 8, 10, 5),
                        stringsAsFactors = FALSE)

mgrMat = array(0, 
               dim = c(3, 3, 2), 
               dimnames = list(c("A", "B", "C"), c("A", "B", "C"), obsDates))

for (i in 1:length(obsDates)){

  migrationsDate = migrations[migrations$obsDate == obsDates[i],]

  for (j in 1:nrow(migrationsDate)){

    mgrMat[migrationsDate$t1[j], migrationsDate$t2[j], i] = migrationsDate$number[j]
  }
}

将两个网址添加到function selectTen(selectedItems) { return _.partialRight(selectedItems, [4]); } 对象:

var URLbase = function ()
{
    return "URL_BASE_FOR_WEBSERVICE";
}

最后在config任务中使用gulp-replacegulp.task('build-development', function (done) { config.minify = false; config.urlBaseForWebService = "http://localhost/MyProject/Service.svc/"; var tasks = ['html', 'resources', 'assets', 'sass', 'js', 'vendors']; seq('clean', tasks, done); }); gulp.task('build-production', function (done) { config.minify = true; config.urlBaseForWebService = "http://myproject.link.pt/Service/Service.svc/"; var tasks = ['html', 'resources', 'assets', 'sass', 'js', 'vendors']; seq('clean', tasks, function() { notifier.notify({ title: 'Project publishment', message: 'Done' }); }); }); 占位符替换为js的值:

URL_BASE_FOR_WEBSERVICE