Angular overall function

Asked

Viewed 346 times

1

Good morning guys, I don’t work much with front-end and I started using angular with bootstrap a little while ago my doubt is this: I have a div for message presentation, error, success; To change these messages according to the data obtained I ended up creating two functions one for success and another for error where $Scope, $timeout and message per parameter is passed and these functions are global, to be reused in other controllers, is that correct? If not what better way to do it, they follow the functions:

/*Mensagens para operações realizadas com sucesso */
function showMsgSuccess($scope, $timeout, msg){
    $scope.mostraMsg = true;
    $scope.classMessage = 'alert alert-info default';
    $scope.message = msg;
    $timeout(function(){
        $scope.classMessage = 'alert alert-info fade';
    }, 4000);       
}

/*Mensagens para operações realizadas com erro */
function showMsgError($scope, $timeout, msg){
    $scope.mostraMsg = true;
    $scope.classMessage = 'alert alert-danger default';
    $scope.message = msg;
    $timeout(function(){
        $scope.classMessage = 'alert alert-danger fade';
    }, 4000);       
}

example call in callback

showMsgError($scope, $timeout, 'Erro ao Buscar Cep!!!');

the html

<div ng-if="mostraMsg" ng-class="classMessage">
        <span class="glyphicon glyphicon-exclamation-sign">  <strong>{{message}}</strong></span>
</div>
  • Good evening friend, I would recommend building directives. ex.: <app-msg error|Success >Error while searching Zip! </app-msg>. Something similar to this. Check out the link. [https://docs.angularjs.org/guide/component]. If you have any questions, just call

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.