angularjs跨路由维护范围变量

angularjs跨路由维护范围变量,angularjs,angularjs-scope,angularjs-service,angularjs-routing,ngroute,Angularjs,Angularjs Scope,Angularjs Service,Angularjs Routing,Ngroute,如何跨路线维护模型。例如,我有一个配置文件列表加载到主页上。主页还包含一个“加载更多”操作来加载更多配置文件,基本上是将数据推送到模型中。单击特定纵断面后,该纵断面的详细视图将通过管线激活。“详细信息”视图有一个后退按钮,可将用户重定向回主页。返回主页时,“加载更多”操作加载的数据(配置文件)丢失。我需要用“加载更多”的前置数据来维护模型 下面是使用的代码 /* Controllers */ var profileControllers = angular.module('profileCont

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

下面是使用的代码

/* 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'
      });
  }]);
/*控制器*/
var profileControllers=angular.module('profileControllers',['profileServices']))
profileControllers.controller('profileListCtrl',['$scope','$location','Profile','$http',
函数($scope、$location、Profile、$http){
$scope.Profiles=Profile.query(函数(){
如果($scope.Profiles.length<3){
$(“#加载更多主页”).hide();
}
});
$scope.orderProp='popular';
$scope.response=[];
//荷德莫尔
$scope.loadmore=function()
{
$http.get('profiles/profiles.php?profile_index='+$('#item list.sub item')。长度)。成功(函数(响应){
如果(答复){
var reposelength=响应长度;
如果(回复长度>1){
$。每个(响应、功能(索引、项目){
$scope.Profiles.push({
UID:response[index].UID,
id:response[index].id,
热门:响应[索引]。热门,
imageUrl:响应[索引]。imageUrl,
名称:响应[索引]。名称,
标记:响应[index]。标记,
类别:响应[索引]。类别
});
});
}
如果(回复长度<3){
$(“#加载更多主页”).hide();
}
}
});
}
}]);
/*应用程序模块*/
var profileApp=angular.module('profileApp'[
"ngRoute",,
“配置文件控制器”,
‘档案服务’,
]);
profileApp.config(['$routeProvider',
函数($routeProvider){
$routeProvider。
当(“/profiles”{
templateUrl:“partials/profile list.html”,
控制器:“profileListCtrl”,
决心:{
deps:函数($q,$rootScope){
var deferred=$q.deferred();
var依赖项=['js/sort.js'];
$script(依赖项、函数(){
$rootScope.$apply(函数(){
延迟。解决();
});
});
回报。承诺;
}
}
}).
当('/profiles/:profileId'时{
templateUrl:'partials/profile detail.html',
控制器:“profileDetailCtrl”,
}).
当('/profiles/cat/:category'时{
templateUrl:“partials/profile list category.html”,
控制器:“profileCategoryListCtrl”,
}).
当(“/create/”时{
templateUrl:“partials/profile create.html”,
控制器:“profileCreateCtrl”,
css:['css/createprofile.css','css/jquery-ui-1.10.4.custom.min.css','css/spectrum.css'],
}).
否则({
重定向到:“/profiles”
});
}]);
您可以使用。服务是单例的,可以跨路由更改存储数据

myModule.factory('serviceId', function() {
  var shinyNewServiceInstance = {
    // your data here
  };
  return shinyNewServiceInstance;
});
并在控制器中使用它:

controller('MyController', function($scope, serviceId) {
  // use serviceId here
});

您正在丢失
$scope
变量,因为在Angular中,
控制器
不是单例。当您返回时,将生成一个新的实例


如果要在路线更改之间存储任何类型的变量,可以创建一个
服务来执行此操作。

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

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


创建角度服务,并在不同路线上传递


这里有详细的答案-

听起来您需要ui路由器,它维护视图状态@MikeRobinson我已将应用程序中使用的代码添加到我的问题中,请您指导我如何使用从load more call to action路由回详细视图到列表视图的预置数据维护列表视图模型?我正在使用一项服务,将初始数据加载到主页(即列表)。loadmore通过$http.get进行调用,将数据推送到模型。如何将其链接到详细视图,如果您能提供一个示例,那就太好了。在详细视图的控制器中,请参考服务(如
MyController
serviceId
示例所示)。您可以跨控制器访问服务中存在的数据。如果不清楚,您可能会提供有关来源的更多详细信息。我已更新问题,以包含控制器、加载更多、使用的服务代码将
$http.get(…)
移动到您的服务并将结果缓存在那里。如果仍然不清楚您可能会创建一个我已经创建了一个plunker,我不确定是否要上传php文件和数据库。你能处理虚拟数据吗。这是一个返回的对象示例{UID:1,id:1,popular:30,imageUrl:“media/images/profile/3.jpg”,name:“Dillion Stac”,tags:[“#anger”,“#dam”,“#smart”],category:[“expression”]}我已经添加了问题中使用的代码,我关心的是loadmore数据,从概要文件的详细视图返回列表视图时,如何使用“加载更多”数据维护模型