Manel Mendez Manel Mendez - 6 months ago 33
Javascript Question

Change ng-show value in other controller

I'm trying to show and hide a div using

. It's a navbar that I want to show only in some views.

I have a controller which "controls" that div. And in other controller I want to edit this
value in order to hide or show the div (navbar).

I tried different things as using a
, a timeout, an
, a factory... but nothing works.

So I'm asking here if anyone could help me.

(Sorry for my English)

This is my html and js codes (last edit code)

<div id="main">
<!-- AquĆ­ inyectamos las vistas -->
<div ng-controller="appCtrl" ng-show="isLogged" class="navbar navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>
<a class="navbar-brand" href="#/">Aula Virtual</a> </div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav" style="text-align: right">
<li class="active"><a href="#/home">Home</a></li>
<li><a href="#/server">Users</a></li>
<li><a href="#/operaciones">Operaciones</a></li>
<li><a href="#/about">About</a></li>
<li><a href="#/contact">Contact</a></li>
<div class="connect">
<div class="container">
Aula Virtual para profesorado y alumnos de la universidad
<div ui-view></div>

I tried a (ng-show="isLogged==false") too.

The controller of the div:

.controller('appCtrl', function($scope, $rootScope) {
console.log($scope.isLogged); //---> this shows undefined

The controller where I want to edit the isLogged value:

cities2.controller('userCtrl',['rootScope', '$scope', '$state','$http','md5', function($rootScope, $scope, $state, $http, md5) {
$rootScope.isLogged = true;

Thanks for the help!


Well, finally I found the solution. I put this if can help someone:

I put a .run in the appCtrl where I initialize the ng-show:

.run(function ($rootScope) {
        $rootScope.isLogged = false;

and now, when I put a true value in the other controller it works, and the navbar appears.

cities2.controller('userCtrl',['$rootScope', '$scope', '$state','$http','md5','$sessionStorage',  function($rootScope, $scope, $state, $http, md5, $sessionStorage) {