Skip to content

Instantly share code, notes, and snippets.

<div ng-controller="navCtrl">
<span>{{user}}</span> <!-- won't be updating -->
<div ng-controller="loginCtrl">
<span>{{user}}</span>
<input ng-model="user"></input> <!-- changes update only loginCtrl scope -->
</div>
</div>
module.service = function(name, Class) {
provider.provide(name, function() {
this.$get = function($injector) {
return $injector.instantiate(Class);
};
});
}
module.factory = function(name, factory) {
provider.provide(name, function() {
module.provider('greeter3', function() {
var salutation = 'Hello';
this.setSalutation = function(s) {
salutation = s;
}
function Greeter(a) {
this.greet = function() {
return salutation + ' ' + a;
}
module.factory('greeterFactory', function(a) {
function Greeter(a) {
this.greet = function() {
return 'Hello ' + a;
}
}
return new Greeter(a);
});
module.factory('b', function(a) {
return a*2;
});
function Controller(b) {
expect(b).toEqual(246);
}
module.value('a', 123);
module.constant('A', 321); //can't modify with a decorator
function Controller(a, A) {
expect(a).toEqual(123);
expect(A).toEqual(231);
}
var underscore = angular.module('underscore', []);
underscore.factory('_', function() {
return window._; //Underscore must already be loaded on the page
});
var app = angular.module('app', ['underscore']);
app.controller('MainCtrl', ['$scope', '_', function($scope, _) {
init = function() {
_.keys($scope);
app.controller('MainCtrl', ['$scope', '$timeout', function($scope, $timeout){
$timeout(function() {
console.log($scope);
});
}]);
//another best bractice approach with $inject
app.controller('MainCtrl', mainCtrl);
'use strict';
let app = angular.module('app',[]);
app.controller('MainCtrl', function($scope, $timeout) { //MainCtrl has dependency on $scope and $timeout
$timeout(function(){
console.log($scope);
}, );
});
//And code after minification:
'use strict';
let sharedServicesModule = angular.module('sharedServices',[]);
sharedServices.service('NetworkService', function($http){});
let loginModule = angular.module('login', ['sharedServices']);
loginModule.service('loginService', function(NetworkService){});
loginModule.controller('loginCtrl', function($scope, loginService){});
let app = angular.module('app', ['sharedServices', 'login']);