angularjs 跨路由维护范围变量

标签 angularjs angularjs-scope angularjs-service angularjs-routing ngroute

如何跨路线维护模型。例如,我有一个加载到主页上的配置文件列表。主页还包含一个“加载更多”操作来加载更多配置文件,基本上将数据推送到模型。单击特定配置文件时,该配置文件的详细 View 将通过路由激活。详细 View 有一个后退按钮,可将用户重定向回主页。在路由回主页时,由“加载更多”操作加载的数据(配置文件)会丢失。我需要使用“加载更多”前置数据来维护模型

下面是使用的代码

/* Controllers */
var profileControllers = angular.module('profileControllers', ['profileServices'])


profileControllers.controller('profileListCtrl', ['$scope','$location', 'Profile','$http',
  function($scope,$location, Profile,$http) {
    $scope.Profiles = Profile.query(function(){

        if($scope.Profiles.length < 3) {
                    $('#load_more_main_page').hide();
                }
        });
    $scope.orderProp = 'popular';
    $scope.response=[];

    //LOADMORE
    $scope.loadmore=function()
    {

        $http.get('profiles/profiles.php?profile_index='+$('#item-list .sub-item').length).success(function(response){
            if(response) {
                var reponseLength = response.length;
                if(reponseLength > 1) {
                    $.each(response,function(index,item) {


                         $scope.Profiles.push({
                                            UID: response[index].UID,
                                            id: response[index].id,
                                            popular: response[index].popular,
                                            imageUrl: response[index].imageUrl,
                                            name: response[index].name,
                                            tags: response[index].tags,
                                            category: response[index].category
                                        });

                        });
                }
                if(reponseLength < 3) {
                    $('#load_more_main_page').hide();
                }
            }

        });


    }

  }]);





  /* App Module */

var profileApp = angular.module('profileApp', [
  'ngRoute',
  'profileControllers',
  'profileServices',
]);



profileApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/profiles', {
        templateUrl: 'partials/profile-list.html',
        controller: 'profileListCtrl',
        resolve: {
            deps: function ($q, $rootScope) {
                var deferred = $q.defer();
                var dependencies = ['js/sort.js'];
                $script(dependencies, function () {
                    $rootScope.$apply(function () {
                        deferred.resolve();
                    });
                });
                return deferred.promise;
            }
        }
      }).
      when('/profiles/:profileId', {
        templateUrl: 'partials/profile-detail.html',
        controller: 'profileDetailCtrl',

      }).
      when('/profiles/cat/:category', {
        templateUrl: 'partials/profile-list-category.html',
        controller: 'profileCategoryListCtrl',

      }).
      when('/create/', {
        templateUrl: 'partials/profile-create.html',
        controller: 'profileCreateCtrl',
        css: ['css/createprofile.css','css/jquery-ui-1.10.4.custom.min.css','css/spectrum.css'],

      }).
      otherwise({
        redirectTo: '/profiles'
      });
  }]);

最佳答案

服务通常是在 View 之间共享数据的公认方式。因为它是一个单例并且不会在路由更改时重新生成,所以您可以在那里“缓存”数据并从服务注入(inject)的任何 Controller 中检索它。

这个问题的第二个答案用代码解释了它:

Same data in multiple views using AngularJS

关于angularjs 跨路由维护范围变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23397896/

相关文章:

javascript - 如何绑定(bind)到 AngularJS 中函数的结果

javascript - 如何从 Nodejs 中的 post 请求中获取正文?

javascript - 在 ng-class 中删除和添加类

javascript - 如何在 ng-repeat 中显示接下来的 15 个项目?

angularjs - 后端 API 分组

angularjs - AngularJS 服务何时重新初始化?

ruby - 获取 TypeError - 无法克隆符号 : error when using active_model_serializer

angularjs - 数据未渲染,Angular JS

angularjs - 从 $scope 获取 Controller 名称

javascript - AngularJS自定义指令在继承父范围的同时访问模板中的属性