I'm trying to share some data from the controller in the current view to my navigation bar. but the data is shared wrong, or not synced correctly.
this is my factory:
myApp.factory('HeaderData', function () { var data = { Visible: true, PageTitle: '' }; return { getVisible: function () { return data.Visible; }, setVisible: function (visible) { data.Visible = visible; console.log("HeaderData: " +visible); }, getPageTitle: function () { return data.PageTitle; }, setPageTitle: function (title) { data.PageTitle = title; } }; }); then in my controllers I'm doing the following:
myApp.controller('homeCtrl',function ($scope, HeaderData) { HeaderData.setVisible(false); console.log("HomeCtrl: " + HeaderData.getVisible()); }); in the Nav controller I read the data in like following:
myApp.controller('navCtrl', function ($scope, HeaderData) { console.log("NavCtrl: " +HeaderData.getVisible()); $scope.showHeader = HeaderData.getVisible(); $scope.pageTitle = HeaderData.getPageTitle(); }); the following output is logged:
NavCtrl: true HeaderData: false HomeCtrl: false So my NavContrl is loaded before my Data is set, and this is logical because it's like this in the HTML:
<div ng-controller="navCtrl"> <ng-include ng-show="showHeader" src="'../partials/common/header.html'"></ng-include> </div> <div ng-view></div> So how can I make it work that my navCtrl updates the data correctly, and in this example hide the header when the $scope.showHeader is set to false?