串联的JavaScript与一饮而尽匿名函数 [英] concatenate javascript in anonymous function with gulp

查看:111
本文介绍了串联的JavaScript与一饮而尽匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一饮而尽到Concat的我的脚本到一个文件中。连接前我已经拆了我的.js文件到单独的文件中。

I use gulp to concat my scripts into one file. Before concatenation I've split up my .js files into seperate files.

所以,我有我的地方我的声明角的应用程序和一些其他的东西app.js。但我也有,我声明我的路由route.js。然而,我的角应用在app.js声明被包装的匿名函数内部,所以当它加到app.js和route.js,route.js落在我的角度应用程序的声明的范围之外。

So I've got app.js where I declare my angular app and some other stuff. But I've also got route.js where I declare my routing. However, the declaration of my angular app in app.js is wrapped inside an anonymous function, so when it concatenates app.js and route.js, route.js falls outside the scope of the declaration of my Angular app.

是否有某种方式串连所有代替注入脚本到的范围与咕嘟咕嘟在一起吗?或者是有一些其他的方法,我很想念,除了didging JSLint的和匿名函数?

Is there some way to inject a script into the scope with Gulp in stead of concatenate it all together? Or is there some other approach I'm missing, apart from didging JSLint and the anonymous function?

推荐答案

如果你不使用CommonJS的或AMD模块,只是串联脚本,然后只是确保每个级联的脚本得到它在它自己的范围内所需要的模块。

If you're not using CommonJS or AMD modules and just concatenating scripts then just make sure each concatenated script gets the module it needs in it's own scope.

(function() {

    // Declare app
    var app = angular.module('Application', ['ngRoute']);

    // Do stuff with variable app in scope.

})();

在routes.js

现在

Now in your routes.js

(function() {

    // Grab the already created module for use
    // in this scope.
    var app = angular.module('Application');

    app.config([
        '$locationProvider', 
        '$routeProvider',
        function($locationProvider, $routeProvider) {

            $locationProvider.html5Mode = true;

            $routeProvider.when('/', {
                controller : 'MyCtrl',
                templateUrl : 'index.html'
            });
        }
    ]);
})() 

这篇关于串联的JavaScript与一饮而尽匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆