...
| Bloco de código | ||||||||
|---|---|---|---|---|---|---|---|---|
| ||||||||
shieldModule.service( 'DialogService', function() {
this.visible = false;
this.isOpen = function() {
return visible;
};
this.show = function() {
this.visible = true;
};
this.hide = function() {
this.visible = false;
};
});
shieldModule.controller( 'UserProfileController', [ 'DialogService', '$scope', function( DialogService, scope ) {
// ...
// ...
// metodo chamado ao clicar no botão link 'Abrir Profile'
scope.openUserProfile = function() {
if ( !DialogService.isOpen() ) {
DialogService.show();
}
};
}]); |
...
| Bloco de código | ||||||||
|---|---|---|---|---|---|---|---|---|
| ||||||||
shieldModule.factory('$dialog', function() {
var Dialog = function(isOpen, operation) {
this.isOpen = isOpen ? isOpen : false;
this.operation = operation ? operation : '';
};
Dialog.prototype.close = function() {
this.isOpen = false;
this.operation = '';
};
Dialog.prototype.open = function(op) {
this.isOpen = true;
if (op) this.operation = op;
};
return {
'new': function(isOpen, operation) {
return new Dialog(isOpen, operation);
}
};
});
shieldApp.controller( 'PeriodCtrl', ['$scope', '$http', '$property', '$resourceLocator', '$dialog', function ( $scope, $http, $property, $resourceLocator, $dialog ) {
// ...
// ...
$scope.view = {
// ...
// ...
detailDialog: $dialog.new()
// ...
// ...
};
// ...
// ...
}]); |
Note a forma que declaramos a função foo na factory acima. Internamente, ao declarar uma factory, a função foo já esta instanciada e apenas será executada, diferentemente do service onde uma nova função será instanciada através do operador new. A factory $dialog oferece um método público new que constroi uma nova instância do objeto Dialog, por isso é preferível utilizar factories, devido a flexibilidade de criação destes objetos.
...