简体   繁体   中英

$rootscope is not accessible in controller

In my code I have 2 controllers 1.BookListCtrl_Librarian 2.IssueBookCtrl

I have an object called Books which I am using in both the controllers.

The issue I am facing is : In IssueBookCtrl controller,i am updating the Books object and the same object is being used to display data in a view using BookListCtrl_Librarian controller.

I used below code line to update the object and hoping that it will display the updated object in the view which is using BookListCtrl_Librarian controller.

$rootScope.books[i].issued = true;

But it gives me error "TypeError: Unable to get property '0' of undefined or null reference"

so as a summary I am not able to access $rootScope here.

Please suggest,any help is much appreciated.

Thanks

Controllers.controller('BookListCtrl_Librarian', ['$scope','$http','$location','$rootScope','BookData',
function ($scope, $http ,$location, $rootScope , BookData) {
    $http.get('data/books.json').success(function(data) {
    if($rootScope.books=='undefined'){
        $rootScope.books = BookData.getData();
    }
        $scope.books = data;
    });
    $scope.options = ['bookId', 'cost'];
    //$scope.query = 'Search';
    $scope.issued=[];
    $scope.visibility=function(index) {
        if($scope.issued[index]==false){
            $scope.issued[index]=true;
            return $scope.issued[index];
        }
        else{
            $scope.issued[index]=false;
            return $scope.issued[index];
        }

    }
    $scope.issue = function(bookId) {
        for (var i = 0, len = $scope.books.length; i < len; i++) {
            if ($scope.books[i].bookId == bookId) {
                $rootScope.book = $scope.books[i];
                break;
            }
        }
        $location.path('/issue/'+bookId);
    }

        $scope.return = function (bookId) {
            for (var i = 0, len = $scope.books.length; i < len; i++) {
                if ($scope.books[i].bookId == bookId) {
                    $rootScope.book = $scope.books[i];
                    break;
                }
            }
            $location.path('/return/'+bookId);
        }
    }]);
Controllers.controller('IssueBookCtrl', ['$scope','$rootScope','$http','$routeParams','$location',
function ($scope,$rootScope, $http, $routeParams, $location) {
    var Id=$routeParams.bookId;
    $http.get('data/books.json').success(function(data) {
        $scope.books = data;
    });
    $scope.bookId=$routeParams.bookId;
    $scope.issue = function(Id) {
        alert("issued");
        for (var i = 0, len = $scope.books.length; i < len; i++) {
            if ($scope.books[i].bookId == Id) {
                $rootScope.books[i].issued = true;
                $location.path('/home/librarian');
            }
        }
    }
}]);

$scope.books[i] is an object, so push that object in the $rootScope.book so that you can access it later via index .

for (var i = 0, len = $scope.books.length; i < len; i++) {
            if ($scope.books[i].bookId == bookId) {
                $rootScope.book.push($scope.books[i]);
                break;
            }
        }
        $location.path('/issue/'+bookId);
    }

Make sure the BookData.getData() is actually being called. In the test

if($rootScope.books=='undefined'){
    $rootScope.books = BookData.getData();
}

You should be checking using typeof . Like this:

if (typeof $rootScope.books === 'undefined') {
   $rootScope.books = BookData.getData();
}

It is likely that your data is undefined, but you are testing for equality to the string 'undefined'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM