var myApp = angular.module('myApp', []);
myApp.value('clientId', 'a12345654321x');
myApp.factory('apiToken', ['clientId',
function apiTokenFactory(clientId) {
var encrypt = function(data1, data2) {
// NSA-proof encryption algorithm:
return (data1 + ':' + data2).toUpperCase();
};
var secret = window.localStorage
.getItem('myApp.secret');
var apiToken = encrypt(clientId, secret);
return apiToken;
}]);
function UnicornLauncher(apiToken) {
this.launchedCount = 0;
this.launch = function() {
// Make a request to the
//remote API and include the apiToken
...
this.launchedCount++;
}
}
myApp.service('unicornLauncher', [
"apiToken",
UnicornLauncher
]);
myApp.provider('unicornLauncher',
function UnicornLauncherProvider() {
var useTinfoilShielding = false;
this.useTinfoilShielding = function(value) {
useTinfoilShielding = !!value;
};
this.$get = ["apiToken",
function unicornLauncherFactory(apiToken) {
return new UnicornLauncher(apiToken,
useTinfoilShielding);
}];
});
myApp.constant('planetName', 'Greasy Giant');
template y templateUrl (opcionales)
angular.module('docsSimpleDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.directive('myCustomer', function() {
return {
template: 'Name: {{customer.name}} Address: {{customer.address}}'
};
});
Definen el HTML que representa a esta directiva.
restrict (opcional)
angular.module('docsRestrictDirective', [])
.directive('myCustomer', function() {
return {
restrict: 'E',
templateUrl: 'my-customer.html'
};
});
Define el tipo de directiva que vamos a crear
Por defecto, el valor es 'AE'
scope (opcional)
angular.module('docsRestrictDirective', [])
.directive('myCustomer', function() {
return {
restrict: 'E',
scope: {
customerInfo: '=info'
},
templateUrl: 'my-customer-iso.html'
};
});
Permite encapsular el scope de la directiva.
link (opcional)
Se utiliza cuando la directiva va a manipular el DOM.
link debe ser una función del siguiente tipo:
function link(scope, element, attrs, controller, transcludeFn) { ... }
link (opcional)
angular.module('docsRestrictDirective', [])
.directive('myCurrentTime', ['$interval', 'dateFilter', function($interval, dateFilter) {
function link(scope, element, attrs) {
var format,
timeoutId;
function updateTime() {
element.text(dateFilter(new Date(), format));
}
scope.$watch(attrs.myCurrentTime, function(value) {
format = value;
updateTime();
});
element.on('$destroy', function() {
$interval.cancel(timeoutId);
});
// start the UI update process; save the timeoutId for canceling
timeoutId = $interval(function() {
updateTime(); // update DOM
}, 1000);
}
return {
link: link
};
}]);
transclude (opcional)
angular.module('docsTransclusionDirective', [])
.directive('myDialog', function() {
return {
restrict: 'E',
transclude: true,
templateUrl: 'my-dialog.html'
};
});
Permite obtener el contenido de la directiva, y colocarlo en otra parte específica (indicada con la directiva ng-transclude dentro de nuestro template)
<div class="alert" ng-transclude>
</div>