Angularjs value $Scope out of view

Asked

Viewed 337 times

0

I’m having a doubt in the Angularjs that I’m not able to solve, my index.html is as follows:

<div ng-show="error" class="alert alert-danger">{{error}}</div>
<div ng-show="success" class="alert alert-success">{{success}}</div>

<div class="content">
   <div ng-view></div>
</div>

To assign value in these views I would normally do the following:

if (response.data.success) {
   $scope.success = response.data.success;
} else if (response.data.error) {
   $scope.error = response.data.error;
}

only by defining the value of $scope.error or $scope.success, the value is not displayed (I think it should be because of the div be out of partial), it is only displayed when I assign the value in $rootScope, but the value keeps being displayed until there is some redirection, it is not cleaned when some ajax request is executed (the automatic of angular).

But in short, my question is, is there any way to display the value of $scope out of partial? if not, as I do to clear the value of the $rootScope without having to refresh the page?

  • the $scope.error is a string? because the ng-show has to receive an expression to know if it displays or not.

  • In Boolean comparisons, javascript considers any non-zero value as true, as you can see in the following code snippet: var teste = "teste";&#xA; if (teste) { &#xA; alert(teste); &#xA; }

  • 2

    I took a look at your code and didn’t see a controller pro index.html, maybe that’s your problem.

1 answer

1


  • Yes, I am using ngRoute, I will put the codes and then update the question.

  • The application is in the repository: https://github.com/dennis90/cotacaoweb/tree/master/web (what matters is in the files: modules/Authentication/controller.js , scripts/app.js and index.html) , it is very interesting, but it’s not quite what I’m looking for, I’m wanting to "inject" that chunk of code (the Divs with the error messages and success) into all of the project’s partitals... into the chunk of code you provided, I would have to redirect to a separate view...

  • 1

    See the updated answer, it might help you. Hug

  • 1

    As Maicon commented anyway you will need a controller in some of the higher Divs the error and Success Divs in your index.html

Browser other questions tagged

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