在路由时,使用angular JS中的Services或Factory将JSON数据从控制器传递到另一个控制器

Pass the JSON Data Between From Controller To another Controller with out using Services or Factory in angular JS while Routing

本文关键字:控制器 数据 JSON 另一个 Factory 路由 使用 angular Services 中的 JS      更新时间:2023-09-26

我在Dashboard页面中有对象,当从仪表板点击Details页面时,它应该重定向到Details页面,我想将JSON对象从Dashboard Controller传递到Details Controller,有没有办法使用Angular JS服务或factory

myApp.controller('dashBoardController', ['$scope', function($scope) {
     $scope.detailsPage = function(Object){

     // I want this object available in details page controller
        $location.path('/detailsPage');
};
}]);
myApp.controller('detailsOageController', ['$scope', function($scope) {
  //  need Object data here
}]);

您可以通过以下方法实现从一个控制器到另一个控制器的数据共享。

  1. 使用服务/工厂(首选)
  2. 使用Rootscope和值将在全局范围内(不推荐)
  3. 发射&广播

https://docs.angularjs.org/api/ng/type/$rootScope。范围