简体   繁体   English

如何在多个控制器之间共享$ scope对象

[英]How to share a $scope object between multiple controllers

I am facing problem while sharing a $scope object between 2 controllers. 我在2个控制器之间共享$ scope对象时遇到问题。

In controller 'IssueBookCtrl',I am updating the books object element value like this. 在控制器“ IssueBookCtrl”中,我正在像这样更新books对象元素的值。

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

Then I am using $emit service to share the $scope object with controller 'BookListCtrl_Librarian'. 然后,我使用$ emit服务与控制器'BookListCtrl_Librarian'共享$ scope对象。

$scope.$emit('update_parent_controller', $scope.books); $ scope。$ emit('update_parent_controller',$ scope.books);

But when I run the view which is using the controller 'BookListCtrl_Librarian',i don't see the updated object. 但是,当我运行使用控制器'BookListCtrl_Librarian'的视图时,我看不到更新的对象。

controller.js controller.js

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.$on("update_parent_controller", function(event, books){
        $scope.books = books;
    });
    $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);
        }
    $scope.backToLogin = function() {
        $location.path("/main");
    }
    }]);
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.issue = function(Id) {
        alert("issued");
       for (var i = 0, len = $scope.books.length; i < len; i++) {             
            if ($scope.books[i].bookId === Id) {
                $scope.books[i].issued = true;              
                $scope.$emit('update_parent_controller', $scope.books);
                $location.path('/home/librarian');
                break;
            }
        }
    }
}]);

Please guide me,any help is much appreciated. 请指导我,非常感谢您的帮助。

Thanks 谢谢

You could try forcing a digest after altering a model: 您可以在更改模型后尝试强制摘要:

$scope.$apply();

But I would recommended that you build a Books service to hold those shared models and logic. 但是我建议您构建一个Books服务来保存那些共享的模型和逻辑。 You can learn about creating your own custom services here . 您可以在此处了解有关创建自己的定制服务的信息

Or you could nest your controllers (put one inside the other) so that the inner controller can reference the outer controller's models by using $parent variable. 或者,您可以嵌套控制器(一个放入另一个),以便内部控制器可以使用$parent变量引用外部控制器的模型。

By using either you should not have any problems with object updating as AngularJs runs dirty-checks when scope variables are changed. 通过使用这两种方法,对象更新不会有任何问题,因为在范围变量更改时AngularJs会进行脏检查。

hi u have to use rootscope instead of scope emit 您好,您必须使用rootscope而不是作用域发射

$rootScope.$emit('update_parent_controller', $scope.books);

and in other controler 和其他控制器

 $rootScope.$on('update_parent_controller', function(event, books){
    $scope.books = books;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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