I am trying to figure out how to update my ng-show
variable when a state is changed. In my default index.html
file I have:
<div id="searchBar" ng-show="searchBar" ng-controller="mainController"></div>
Since this is not part of my page1 template, searchBar
does not get updated when a state is changed. The variables are actually changing correctly, but ng-show
does not know this.
I'm knew to angular, but does anyone have any suggestions on getting ng-show
to notice the change in searchBar
when it is changed in a 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 it be known, that from the below answer, you do not need to have a service to do this.