I wonder if PHP has any equivalence of IIFE (Immediately Invoked Function Expression) like that of Javascript. Can PHP Closure be written anyhow so it can work similarly to Javascript (invoking, dependency, injection, directive) ?
(function(){
myModule = angular.module('myAngularApplication', []);
}());
This expression above is known as Immediately invoked function expression(IIFE). Since the function definition will immediately invoke itself whenever the .js file is loaded. The main reason the IIFE is effective is that we can have all the code immediately executing without needing to have global variables and functions. Now when we do this, our controller creation will fail as we were using the global variable to create controller with the module. To circumvent this problem lets use the getter function angular.module to associate the controller with the module. And while we are at it, why not put the controller in an IIFE too.
(function () {
var booksController = function ($scope) {
$scope.message = "Hello from booksController";
}
angular.module('myAngularApplication').controller('booksController', booksController);
}());
Source: http://www.codeproject.com/Articles/995498/Angular-Tutorial-Part-Understanding-Modules-and
Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…