Refresh page without losing the value of a variable

0

I have a vector that receives data coming from a socket that I use for notifications to the user, however every time I change pages I lose all the value coming from this variable, I'm using angular on the front end. How can I navigate the system normally without losing the values inside my vector? PS: If anyone knows of any logic, tutorial related to angular and notifications (facebbok style, twitter, etc), can you pass me that I will study. Thanks

    
asked by anonymous 04.03.2017 / 15:26

2 answers

0

You can create a service and store it, because when the page is updated the service is not restarted.

    angular.module('app', [''])
  .service('myService', myService);

  function myService($http) {
    var dados;

    function setDados(valor) {
      dados = valor;
    }

    function getDados() {
      return dados
    }

  }

After that, just inject the service on your controller and access the functions

angular.module('heroApp', [])
  .controller('myCtrl', myCtrl)

  function myCtrl($scope, myService) {
    var contato = {
      email:'[email protected]',
      nome:'Fulano'
    }

    myService.setDados(contato);

    console.log(myService.getDados())

  }
    
13.03.2017 / 21:39
0

This example illustrates the basic creation of a session anchored to a global variable. But the overuse of global variables brings serious application risks. The comments above a colleague indicates a $_SESSION study material I suggest you study this before implementing. Also this Link . I hope I have helped you study well.

      <?php
      session_start();

      $st=mysqli_query($conn,"select * from usuarios where login='$doc'");
      $result=mysqli_fetch_array($st);

      $_SESSION['usuario'] = $result['nome'];
  ?>
<html>
 <head>
</head>
<body>
    <?php
    if ($_SESSION['usuario'] === false){
      Session_destroy();
      header("location: index"); exit;
    }
        <li class="dropdown">
            <a href="" class="dropdown-toggle" data-toggle="dropdown"><?php  echo $_SESSION['usuario'] ?> <span class="caret"></span></a>
                <ul class="dropdown-menu">
                <li><a href="editarperfil">Editar</a></li>
                <li><a href="logout">Sair</a></li>
            </ul>
        </li>
    </ul>
    ?>
</body>

    
05.03.2017 / 01:31