簡體   English   中英

$ rootscope在控制器中不可訪問

[英]$rootscope is not accessible in controller

在我的代碼中,我有2個控制器1.BookListCtrl_Librarian 2.IssueBookCtrl

我在兩個控制器中都使用了一個名為Books的對象。

我面臨的問題是:在IssueBookCtrl控制器中,我正在更新Books對象,並且使用BookListCtrl_Librarian控制器使用同一對象在視圖中顯示數據。

我使用下面的代碼行來更新對象,並希望它將在使用BookListCtrl_Librarian控制器的視圖中顯示更新的對象。

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

但這給我錯誤“ TypeError:無法獲取未定義或空引用的屬性'0'”

因此,作為摘要,我無法在此處訪問$ rootScope。

請提出建議,任何幫助將不勝感激。

謝謝

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]是一個對象,因此將該對象推$scope.books[i] $rootScope.book以便以后可以通過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);
    }

確保BookData.getData()被實際調用。 在測試中

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

您應該使用typeof進行檢查。 像這樣:

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

您的數據可能未定義,但是您正在測試是否與字符串“ undefined”相等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM