问题
I use gulp to concat my scripts into one file. Before concatenation I've split up my .js files into seperate files.
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.
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?
回答1:
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.
})();
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'
});
}
]);
})()
来源:https://stackoverflow.com/questions/28320122/concatenate-javascript-in-anonymous-function-with-gulp