javascript - Updating ng-show from different controller -


i trying figure out how update ng-show variable when state changed. in default index.html file have:

<div id="searchbar" ng-show="searchbar" ng-controller="maincontroller"></div> 

since not part of page1 template, searchbar not updated when state changed. variables changing correctly, ng-show not know this.

i'm knew angular, have suggestions on getting ng-show notice change in searchbar when changed in controller?

var routerapp = angular.module('app', ['ui.router'])     .service('mainshared', function () {         var mainshared = this;         mainshared.searchbar = false;     })     .controller('maincontroller', function($scope, mainshared) {         $scope.searchbar = mainshared.searchbar;     });  routerapp.config(function($stateprovider, $urlrouterprovider, $locationprovider) {      $locationprovider.html5mode(true);     $urlrouterprovider.otherwise('/page1');      $stateprovider         .state('page1', {             url: '/page1',             templateurl: 'pages/templates/page1.html',              controller: 'page1'          }); });  routerapp.controller('page1', function($scope, $http, $state, mainshared) {      mainshared.searchbar = true;  }); 

edit: let known, below answer, not need have service this.

use $rootscope

.controller('maincontroller', function($scope, $rootscope) {     console.log($rootscope.searchbar); });   routerapp.controller('page1', function($scope, $rootscope, $http, $state) {     $rootscope.searchbar = true; });