简体   繁体   English

在页面加载之前加载Angular JS数据

[英]Angular JS Data loading Before Page Loading

I wanna load data before AngularJS Page Load. 我想在AngularJS Page Load之前加载数据。 I'm using bootstrap and JQuery as well along Angualr. 我在Angualr上也使用bootstrap和JQuery。 Main problem is that i have table on page and used JQuery for pagination and other filters including responsiveness as well. 主要问题是我在页面上有表格,并使用JQuery进行分页和其他过滤器,包括响应性。

Now what happened is, that Page loads all JQuery and later my data comes on page and nothing works like pagination etc. I do following workaround: 现在发生了什么,该页面加载了所有JQuery,后来我的数据出现在页面上,没有什么像分页等。我做了以下解决方法:

 <script>
      $(function () {
        //  document.body.style.overflow = 'hidden';
          setTimeout(
                  function() 

                      $('#example1').DataTable({
                          "paging": true,
                          "lengthChange": false,
                          "searching": true,
                          "ordering": true,
                          "info": true,
                          "autoWidth": true,
                          "scrollX": true
                        });
                  }, 500);

      });
    </script>

I added manual delay to wait JQuery, so that page loads its data before. 我添加了手动延迟等待JQuery,以便该页面之前加载其数据。

Can anybody tells good solution, how to make sure that data is loaded before Page HTML renders ? 任何人都可以告诉好的解决方案,如何确保在页面HTML呈现之前加载数据?

I'd strongly suggest against using jQuery for your pagination. 我强烈建议不要使用jQuery进行分页。 There are other solutions that will keep the Angular magic working for you such as ui-grid, smart-table, etc. These require pretty low effort on your part. 还有其他解决方案可以让Angular魔法为你工作,比如ui-grid,smart-table等。这些都需要你很少的努力。

If you really want to keep on this path though, you might be able to get what you want with $routeProvider.when 's resolve. 如果你真的想继续沿着这条道路前进,那么你可以通过$routeProvider.when的决心获得你想要的东西。 When the route is requested, the resolve block will run any promises first, and wait for them to resolve before going to that route. 当请求路由时,解析块将首先运行任何promise,并在转到该路由之前等待它们解析。 Resolve can also pass that data into the controller of the route: Resolve还可以将该数据传递到路径的控制器中:

.when('/route', {
    ...,
    resolve: {
        dataYouWant: function(DataGettingService) {
            return DataGettingService.get();
        }
    }
})

routeProvider routeProvider

This is just a possible solution. 这只是一种可能的解决方案。 You might also use a directive instead of a controller 您也可以使用指令而不是控制器

angular
    .module('app', [])
    .factory('repository', function ($http) {
        var loadData = function () {
            return $http... ;//make http call here and return the promise
        };

        return {
            loadData: loadData
        };
    })
    .controller('myCtrl', function (repository) {
        repository.loadData().then(function(data) {
            // use loaded data here
            $('#example1').DataTable({
                "paging": true,
                "lengthChange": false,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": true,
                "scrollX": true
            });
        });

    });

You need to bind your jQuery code with Angular in some way. 您需要以某种方式将您的jQuery代码与Angular绑定。 Here is the directive try. 这是指令尝试。

app.directive('jqueryCallDirective' , function() {
   return {
        link: function(scope, element, attrs) {
            element.DataTable({
                "paging": true,
                 ...
            });
        }
   }
});

Then use ng-if deferred render for your element: 然后对元素使用ng-if延迟渲染:

<div id="example1" ng-if="hasData" jquery-call-directive> ... </div>

and then set $scope.hasData = true in appropriate time on appropriate scope (or just on $rootScope if this is acceptable). 然后在适当的范围$scope.hasData = true适当的时间设置$scope.hasData = true (如果可以接受的话,只在$ rootScope上设置)。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM