last day (14 days later) » 

12:22 PM
2
A: angularJS: Error: $scope is not defined

SarathyThe problem is that you are using $scope within the function which loads notifications. $scope will not be available there since it might not be created yet. You need to return a promise and the resolved value can be injected as a dependency to the controller. var countAllUnreads = function($loc...

 
What do you mean by dependancy for notifications? Should that be a service or a call. i guess not. Or, is there a way I can store this in controller? @Sarathy
 
Not sure how you declare your controllers. Just like $http, $scope, you need to add an additional parameter named 'notifications' for the controller which should match the property name provided in your route configuration for 'resolve'. Have a look at the example for HomeController I had provided in the answer. The result of your service call in method countAllReads will be injected by Angular as 'notifications'. No need for any additional service call.
Look at this post by Scott Allen. It should help. odetocode.com/blogs/scott/archive/2014/05/20/…
 
But this controoler where I want value to be shown is a common controller..It's a controller for header...so no navigation occurs to it. so, it's not listed in routes. @Sarathy
 
Is this header the same for authenticated pages and unauthenticated pages?
 
yes, header is same for all authenticated and unauthenticated pages except for login. @Sarathy
 
12:22 PM
hello
If you need to load data for every route change, then it is not a good idea to keep the header same for all authenticated and unauthenticated pages
 
Actually, header has a few links like logout and all
 
But if you want to proceed with the same approach, then you need to either use $routeChangeStart or $routeChangeSuccess to load the data only for required pages. This event subscription needs to be done in your HeaderController
 
And now i have to show number of messages..actually count
 
So every time the route changes, you can get the latest actual count from service
 
But how does one use $routeChangeStart or $routeChangeSuccess?
 
let me look at it
 
// Route Configuration changes for all authenticated routes. Remove resolve and add a property 'isSecure'

.when('/home', {
controller: 'HomeController',
templateUrl: 'app/components/home/homeView.html',
controllerAs: 'vm',
isSecure: true
}

function HeaderController($scope, $route, yourService) {
$scope.$on('$routeChangeSuccess', function (event, current, previous) {
if ($route.current.$$route.isSecure) {
yourService.getNotificationCount();
}
};
}
Again the above is only one way
If you already have a mechanism that detects whether you are authenticated or not, then use that instead of 'isSecure'
Simply, you can check if ($rootScope.isLoggedIn), then load notifications
 
ok..i will try this
 
12:49 PM
Instead of $scope.$on(), use $rootScope.$on()
and ass $rootScope as a parameter / dependency for HeaderController
 
i am getting route is undefined in ($route.current.$$route.isSecure) {
 
1:04 PM
In your header controller, do you have $route as a parameter?
Sorry that was *add. Typo
 
no
i added $route in my header controller like this
.controller('CommonController', function($scope, $routeParams, myCommonService, AuthService,AuthService3,$rootScope,$routr) {
*$route
like this
.controller('CommonController', function($scope, $routeParams, myCommonService, AuthService,AuthService3,$rootScope,$route) {
 
Just check if($route.current && $route.current.$$route) {}
before accessing $route. It might not be available always
 

last day (14 days later) »