apasen2014-06-10 08:59:52
apasen, 2014-06-10 08:59:52

AngularJS $scope

There is a controller:

angular.module('jsRouter.controllers', [])
        .controller('router', ['$rootScope', function($rootScope){
            $rootScope.isExistRoute =  function (route){
                return $rootScope.routes[route] !== 'undefined'? true: false;
        .run(['$rootScope', 'loadRoutes', function ($rootScope, loadRoutes){
            loadRoutes.get().then(function (response){
                $rootScope.routes = response.data;

There is a directive:
        .directive('routerHref', ['$rootScope', function ($rootScope){
            return {
                restrict: 'A',
                replace: true,
                scope: true,
                link: function ($scope, element, attrs){

In the link of the directive, I output $rootScope to the console:
Scope {$id: "002", $$childTail: $$childScopeClass, $$childHead: $$childScopeClass, $$prevSibling: null, $$nextSibling: null…}
$$asyncQueue: Array[0]
$$childHead: $$childScopeClass
$$childScopeClass: function () {
$$childTail: $$childScopeClass
$$destroyed: false
$$isolateBindings: Object
$$listenerCount: Object
$$listeners: Object
$$nextSibling: null
$$phase: null
$$postDigestQueue: Array[0]
$$prevSibling: null
$$watchers: null
$id: "002"
$parent: null
$root: Scope
routes: Array[5]
this: Scope
__proto__: Scope

I see that the object has an array with routes, I replace console.log($scope.$parent) with console.log($scope.$parent.routes) in the directive - it writes undefined!!!
What am I doing wrong??

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Alexander, 2014-06-10

Most likely, this array of routes is obtained from the server, and at the very moment when console.log($scope.$parent.routes) is triggered, it does not matter yet. In the first case, by the time you view the object displayed in the console, it has time to load.
But again, this is just a guess. The overall picture can be assessed by looking at the entire code and not at a specific passage.

Sergey, 2014-06-10
Protko @Fesor

If you have things like "$scope.$parent" then you are clearly doing something wrong.

maxaon, 2014-06-10

Without the full code, it is extremely difficult to deal with scopes.
Try $scope.$parent.$parent, maybe another directive creates an isolated scope. And where do you output $rootScope?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question