Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs跨路径维护范围变量大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何跨路线维护模型.例如,我有一个加载到主页的配置文件列表.主页还包含“加载更多”操作以加载更多配置文件,基本上将数据推送到模型.单击特定配置文件时,将通过路径激活该配置文件的详细视图.详细视图有一个后退按钮,可将用户重定向回主页.在路由回到主页时,由“加载更多”动作加载的数据(配置文件)将丢失.我需要使用“加载更多”前置数据来维护模型

以下是使用的代码

/* 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

大佬总结

以上是大佬教程为你收集整理的angularjs跨路径维护范围变量全部内容,希望文章能够帮你解决angularjs跨路径维护范围变量所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。