Árvore de páginas

Versões comparadas

Chave

  • Esta linha foi adicionada.
  • Esta linha foi removida.
  • A formatação mudou.

...

Bloco de código
themeEclipse
languagejs
titleRegistrando e Utilizando Services
linenumberstrue
shieldModule.service( 'DateUtil', function() {
	
	this.format = function( date ) {
		new Date(date).format('dd-mm-yyyy');
	};	
});
 
shieldModule.controller( 'PeriodController', [ 'DateUtil', '$scope', function( DateUtil, scope ) {
	
	// ...
	// ...
 
	// metodo chamado para formatar a data escolhida em um widget calendar
	scope.formatCurrentDate = function(date) {
		scope.formattedDate = DateUtil.format(date);
	};
	
}]);

...

Bloco de código
themeEclipse
languagejs
titleRegistrando e Utilizando Factories
linenumberstrue
angular.module('treelayer.commons.ui').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. Factories fornecem mais flexibilidade na sua instanciação, possibilitando a construção de objetos que possuam parametro, por isso, em casos como mostrado no exemplo acima são preferíveis ao invés de services.

...

Bloco de código
themeEclipse
languagejs
titleRegistrando e Utilizando Providers
linenumberstrue
angular.module('treelayer.commons.ui').provider('$message', function () {
	// ... 
	// ...
	this.baseUrl = '';
 
	// ...
	// ...
 
	this.$get = ['$q', '$http', function ($q, $http) {
        return this.$message($q, $http);
    }];
 
	// ...
	// ...
});

Todo provider deve possuir um método $get. Este será invocado pelo AngularJS para instanciar o provider e deixa-lo disponível para o bloco de configuração da aplicação. Internamente, services e factories são instancias de providers, onde o método $get é injetado automaticamente pelo framework para facilitar o desenvolvimento.

...

Bloco de código
themeEclipse
languagejs
titleUtilizando Providers em Blocos de Configuração
linenumberstrue
shieldApp.config(['$messageProvider',
                  '$routeProvider',
                  'partialsPath',
                  function ($messageProvider, $routeProvider, partialsPath) {

	// ...
	// ...

    $messageProvider.baseUrl = AJS.contextPath() + '/rest/shield/latest';

	// ...
	// ...

}]);

Âncora
what-is-directive
what-is-directive

...