// main.js file
var myAppModule = angular.module('myApp', []);
// HTML template
<html ng-app="myApp">
...
</html>angular.module('myModule', []); // create module myModule
angular.module('myModule')
.config(function(injectables) { // provider-injector
// This is an example of config block.
// You can have as many of these as you want.
// You can only inject Providers (not instances) into config blocks.
})
.run(function(injectables) { // instance-injector
// This is an example of a run block.
// You can have as many of these as you want.
// You can only inject instances (not Providers) into run blocks
});myApp.constant('planetName', 'Greasy Giant');
myApp.config(function(myAppProvider, planetName) {
myAppProvider.stampText(planetName);
});// factory definition
var myApp = angular.module('myApp', []);
myApp.factory('clientIdFactory', function clientIdFactory() {
return 'a12345654321x';
});
// obtaining factory 'clientIdFactory' by DI
myApp.controller('DemoController', function (clientIdFactory) {
this.clientId = clientIdFactory;
});function LaunchAppService(token) {
this.launchedCount = 0;
this.launch = function() {
// Make a request to the remote API and include the token
...
this.launchedCount++;
}
}
// factory definition of service
myApp.factory('LaunchAppService', function(token) {
return new LaunchAppService(token);
});
// service definition, LaunchAppService is invoked by constructor injection
myApp.service('LaunchAppService', LaunchAppService);myApp.provider('MyTranslateService', function () {
var lang = 'en';
this.setPreferredLang = function(value) {
lang = value;
};
this.$get = function () {
return new MyTranslateServiceImpl(lang);
};
});
myApp.config(function(MyTranslateServiceProvider) {
MyTranslateServiceProvider.setPreferredLang('en');
});
function MyTranslateServiceImpl(lang) {
this.translate = function(text) {
return lang.translate(text);
};
}myApp.controller('DemoController', function (clientId) {
this.clientId = clientId;
});
myApp.filter('greet', function() {
return function(name) {
return 'Hello, ' + name + '!';
};
});
// HTML template
<div ng-app="myApp">
<div>
{{ 'World' | greet }} // output Hello, World!
</div>
</div>var myApp = angular.module('myApp',[]);
// inject $scope service from Angular -> controller scope is created representing model
myApp.controller('GreetingController', function($scope) {
$scope.greeting = 'Hola!';
});
// HTML template
<div ng-controller="GreetingController">
{{ greeting }} // display Hola! from controller scope
</div>
-----------------------------------------------------------------------------------------
// adding custom behavior to scope
myApp.controller('GreetingController', function($scope) {
$scope.tripple = function (val) { return val*val*val; };
});
// HTML template
<div ng-controller="GreetingController">
2*2*2 = {{ tripple(2) }} // display 2*2*2 = 8
</div>