Created
June 7, 2016 11:13
-
-
Save eduavila/629113035935643106c9d04513200548 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| angular | |
| .module('app.directives') | |
| .directive('selectEntidade',['$uibModal','$http',function($uibModal,$http) { | |
| return { | |
| restrict: 'E', | |
| require: 'ngModel', | |
| scope:{ | |
| result:"=", | |
| ngModel: '=', | |
| }, | |
| templateUrl: 'views/tpls/select-entidade.html', | |
| link: function (scope, element, attrs,ngModelController) { | |
| function setAsLoading(bool) { | |
| ngModelController.$setValidity('entidadeLoading', !bool); | |
| }; | |
| function setAsAvailable(bool) { | |
| ngModelController.$setValidity('entidadeCheck', bool); | |
| }; | |
| angular.element('input').on('blur',function(){ | |
| var value = scope.ngModel; | |
| if(!value || value.length == 0) return; | |
| setAsLoading(true); | |
| setAsAvailable(false); | |
| return $http.get('/api/entidades/'+ value) | |
| .then(function(response){ | |
| precherCampos(response.data); | |
| setAsLoading(false); | |
| setAsAvailable(true); | |
| }) | |
| .catch(function(){ | |
| setAsLoading(false); | |
| setAsAvailable(false); | |
| limparCampos(); | |
| }); | |
| }); | |
| scope.modalEntidade = function () { | |
| // Instancia janela modal. | |
| var modalEntidade = $uibModal.open({ | |
| animation:true, | |
| templateUrl: 'views/tpls/modal_entidade.html', | |
| controller:'ModalEntidadeController', | |
| controllerAs: 'vm', | |
| size: 'lg' | |
| }); | |
| modalEntidade.result.then(function (entidade) { | |
| if(entidade) | |
| { | |
| precherCampos(entidade); | |
| } | |
| }); | |
| }; | |
| function limparCampos(){ | |
| scope.ngModel = ""; | |
| scope.result.razao_social =""; | |
| scope.result.cgc = ""; | |
| scope.result.nome_fantasia = ""; | |
| } | |
| function precherCampos(entidade){ | |
| scope.ngModel = entidade.cod; | |
| scope.result.razao_social = entidade.razao_social; | |
| scope.result.cgc = entidade.cgc; | |
| scope.result.nome_fantasia = entidade.nome_fantasia; | |
| } | |
| } | |
| } | |
| }]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment