angularjs跨路径维护范围变量
发布时间:2020-12-17 17:38:04 所属栏目:安全 来源:网络整理
导读:如何跨路线维护模型.例如,我有一个加载到主页的配置文件列表.主页还包含“加载更多”操作以加载更多配置文件,基本上将数据推送到模型.单击特定配置文件时,将通过路径激活该配置文件的详细视图.详细视图有一个后退按钮,可将用户重定向回主页.在路由回到主页时
如何跨路线维护模型.例如,我有一个加载到主页的配置文件列表.主页还包含“加载更多”操作以加载更多配置文件,基本上将数据推送到模型.单击特定配置文件时,将通过路径激活该配置文件的详细视图.详细视图有一个后退按钮,可将用户重定向回主页.在路由回到主页时,由“加载更多”动作加载的数据(配置文件)将丢失.我需要使用“加载更多”前置数据来维护模型
以下是使用的代码 /* 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' }); }]); 解决方法
服务通常是在视图之间共享数据的可接受方式.因为它是单例并且在路由更改时不会重新生成,所以您可以在那里“缓存”数据并从注入服务的任何控制器中检索它.
这个问题的第二个答案用代码解释: Same data in multiple views using AngularJS (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |