0
I am developing a system for a snack bar, where she will have to register the cities and within the cities the neighborhoods with their due freight, the problem is occurring the first time that occurs the registration of the first neighborhood of the city, because it follows all logic and tries to reload the function carregarBairros()
that is inside another controller
, as shown below:
html code
<div ng-hide="consultaativa" class="catalogo md-whiteframe-1dp col-md col-xs-12 col-sm-12 padding-zero" >
<div class="texto_catalogo" id="catalogo_{{cidade.cid_atd_id}}" style="padding-top: 30px;">
<form name="bairroForm" >
<div style="height:70px;" ng-model="box_alterar" ng-hide="hideAlterar" class="box_alt_bairros">
<div layout-gt-sm="row" style="width:650px;">
<md-input-container flex ng-class="{'md-input-invalid': validaBairro.nomeinv}" >
<label>Bairro</label>
<input class="moeda" style="width: 300px;" ng-blur="validarBairro(1, validaBairro.nome)" ng-click="validarBairro(0, validaBairro.nome)" ng-keyup="validarBairro(0, validaBairro.nome)" type="text" name="bairro" ng-model="validaBairro.nome" autofocus />
<div class="error" ng-if="validaBairro.nomeinv" role="alert" multiple>
<div ng-message="required" class="my-message">Preencha o campo Bairro</div>
</div>
</md-input-container>
<md-input-container flex ng-class="{'md-input-invalid': validaFrete.freteinv}">
<label>Frete</label>
<input ng-model="validaFrete.frete" name="frete" ng-blur="validarFrete(1)" ng-click="validarFrete(0)" ng-keyup="validarFrete(0)" id="valor" mask-money style="width: 300px;padding: 0px;" type="text"/>
<div class="error" ng-if="validaFrete.freteinv" role="alert" multiple>
<div ng-message="required" class="my-message">Preencha o campo Frete</div>
</div>
</md-input-container>
</div>
</div>
<div class="col-md-3 teste padding-zero pull-right" style="margin-top: 10px;">
<section layout="row" layout-sm="column" class="pull-right" layout-wrap>
<md-button ng-click="cancelarAlteracao()" ng-if="alterar" class="md-raised md-warn pull-right">
<i class="fa fa-close"></i>
Cancelar
</md-button>
<md-button ng-click="validaForm(validaBairro.nome, cidade.cid_atd_id)" type="submit" ng-submit="validaForm(validaBairro.nome, cidade.cid_atd_id)" class="md-raised md-primary pull-right">
<i class="fa fa-save"></i>
Salvar
</md-button>
<div class="label"></div>
</section>
</div>
</form>
<div style="height: 50px;"></div>
</div>
</div>
$scope.validaForm = function (bairro, cidade) {
var valor = $("#valor").val();
if ($scope.validaBairro.nomeinv === false && $scope.validaFrete.freteinv === false && $scope.validaBairro.nome !== '' && valor !== '0,00' && valor !== '') {
if ($scope.alterar === false) {
$scope.adicionarBairro(bairro, cidade, valor);
} else {
$scope.updateBairro($scope.id_bairro, cidade, bairro, valor);
}
$scope.validaBairro.nomeinv = false;
$scope.validaFrete.freteinv = false;
} else {
$scope.validarBairro(1, bairro);
$scope.validarFrete(1);
}
};
$scope.adicionarBairro = function (bairro, cidade, valor) {
$http.post(url_sistema + 'mostrarcidades/cadastrar_bairro', {'nome': bairro, 'cidade': cidade}).success(function (data, status, headers, config) {
delete $scope.bairro; // se nao colocar esta linha, quando alterar o campo ele altera o dado da tabela tambem
if (data < 1) {
$http.post(url_sistema + 'mostrarcidades/cadastrar_frete', {'nome': bairro, 'cidade': cidade, 'valor': valor}).success(function (data, status, headers, config) {
delete $scope.bairro;
}).error(function (data, status) {
console.log(data);
});
$mdToast.show(
$mdToast.simple()
.content('Cadastrado com sucesso!')
.theme("success-toast")
.position("top right")
.hideDelay(2000)
);
Scopes.get('mostrarCidadesCtrl').carregarBairros(valor, cidade);
carregarFretes(cidade);
$scope.cancelarAlteracao();
$timeout(function () {
$scope.selectedIndex = 0;
}, 10000);
} else {
$mdDialog.show(
$mdDialog.alert()
.clickOutsideToClose(true)
.title('O bairro informado já está cadastrado!')
.ariaLabel('Existe!')
.ok('ok')
.targetEvent(event)).then(function () {
});
}
}).error(function (data, status) {
console.log(data);
});
};
});
$scope.carregarBairros = function (id_frete, id_cidade) {
$http.post(url_sistema + 'mostrarcidades/listar_bairros', {'id_frete': id_frete, 'id_cidade': id_cidade}).success(function (data, status, headers, config) {
$scope.bairros = data;
console.log(id_cidade);
if ($scope.bairros.length === 0) {
$scope.ativo = true;
} else {
$scope.ativo = false;
}
$timeout(function () {
Scopes.get('fretesCtrl').bairro(id_cidade);
}, 3000);
}).error(function (data, status) { // called asynchronously if an error occurs
console.log(data);
});
};
});
print after clicking save
print after pressing F5
You don’t need to insert all your
source code
here. Post only the code referring to the problem, the functions that involve the problem you are facing. It is easier to identify than having to read all your code just to look for the functions related to it.– celsomtrindade
Sorry Celsom, it was not my intention, first time I use a forum to help me, can help me with this problem?
– Tales Born
Yes, no problem, but do a summary of your Anglican codes, because there is a lot there. Just leave the functions related to the problem
– celsomtrindade
Okay, I’ve adjusted it so that you’re better off understanding, as requested
– Tales Born
But it’s still hard to understand.. What is the function to save the new neighborhood? Where is it?
– celsomtrindade
it calls the function validaForm, from this function it validates whether it is fit to add the new neighborhood or not and calls the function add if it is fit.
– Tales Born
Ok, but friend, edit your question and leave ONLY the codes relevant to your question and problem. Ex.: we don’t need your code
.run
, or definitions of $Scope that have no connection with the function to add neighborhood, or your Factory, for example. Clear your question by leaving only the functions related to the problem. It will be easier to help you.– celsomtrindade
Melhor Celsom?
– Tales Born
Yes, I’ll analyze it and I’ll answer =D
– celsomtrindade
Grateful, I await your answer :P
– Tales Born