The way to define common code in angular is through Services.
You would define a new service like so :
.factory('CommonCode', function ($window) {
var root = {};
root.show = function(msg){
$window.alert(msg);
};
return root;
});
In your controller you would inject this service..like so
function MainAppCtrl($scope,CommonCode)
{
$scope.alerter = CommonCode;
$scope.alerter.show("Hello World");
}
Just include CommonCode as an argument to your controller function.. Angular will take care of injecting it for you ( Read on Dependancy Injection ..to understand what is happening here. )
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…