在这里,我们将给大家分享关于在页面滚动上延迟加载Angular视图和控制器的知识,让您更了解页面滚动加载如何实现的本质,同时也会涉及到如何更有效地Angular4延迟加载参数、angular–在模块延
在这里,我们将给大家分享关于在页面滚动上延迟加载Angular视图和控制器的知识,让您更了解页面滚动加载如何实现的本质,同时也会涉及到如何更有效地Angular 4延迟加载参数、angular – 在模块延迟加载时显示加载动画?、angular – 如何在延迟加载的模块中提供服务,并将该服务限定为延迟加载的模块及其组件?、angular – 延迟加载的DLL的内容。
本文目录一览:- 在页面滚动上延迟加载Angular视图和控制器(页面滚动加载如何实现)
- Angular 4延迟加载参数
- angular – 在模块延迟加载时显示加载动画?
- angular – 如何在延迟加载的模块中提供服务,并将该服务限定为延迟加载的模块及其组件?
- angular – 延迟加载的DLL
在页面滚动上延迟加载Angular视图和控制器(页面滚动加载如何实现)
我有一个利用Laravel和Angular的微型网站。这是一个具有响应能力的一页微型网站,分为5个部分。我想延迟加载它们以减少一次加载。
<body ng-app> <div id="wrapper"> <section id="intro">1</section> <section id="Second">2</section> <section id="Third">3</section> <section id="Fourth">4</section> <section id="Fifth">5</section> </div></body>
我希望在页面加载时加载1和2,然后在您向下滚动页面时加载带有很好的淡入度的另一个视图,然后加载其交互项。
答案1
小编典典在这种情况下,可能没有必要(或没有效率)延迟加载控制器,但是可以做到这一点。
这里有很多事情要解决,所以我将分节处理。
滚动上的延迟加载视图(动画)。
现场演示在这里(单击)。
标记:
<div> <section ng-repeat="section in loadedSections" ng-include="section+''.html''" scroll-load scroll-load-from="sections" scroll-load-to="loadedSections" ng-animate="{enter:''section-animate-enter''}" ></section></div>
动画CSS:
.section-animate-enter { -webkit-transition: 1.5s linear all; transition: 1.5s linear all; opacity: 0; left: 100%;}.section-animate-enter.section-animate-enter-active { opacity: 1; left: 0;}
角度逻辑:
app.controller(''myCtrl'', function($scope) { $scope.sections = [''top'',''mid'',''bottom'']; //html files to load (top.html, etc) $scope.loadedSections = [$scope.sections[0]]; //loaded html files});app.directive(''scrollLoad'', function($compile) { return { restrict: ''A'', link: function(scope, element, attrs) { var to = scope[attrs.scrollLoadTo]; //$scope.loadedSections var from = scope[attrs.scrollLoadFrom]; //$scope.sections $window = angular.element(window); $window.bind(''scroll'', function(event) { var scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0; var scrollPos = scrollTop + document.documentElement.clientHeight; var elemBottom = element[0].offsetTop + element.height(); if (scrollPos >= elemBottom) { //scrolled to bottom of scrollLoad element $window.unbind(event); //this listener is no longer needed. if (to.length < from.length) { //if there are still elements to load //use $apply because we''re in the window event context scope.$apply(to.push(from[to.length])); //add next section } } }); } };});
延迟加载的控制器和滚动视图(动画)。
现场演示在这里(单击)。
标记:
<div> <!-- the "lazy" directive will get the controller first, then add ng-include --> <section ng-repeat="section in loadedSections" lazy="section" scroll-load scroll-load-from="sections" scroll-load-to="loadedSections" ng-animate="{enter:''section-animate-enter''}" ></section></div>
角度逻辑:
var $appControllerProvider; //see belowvar app = angular.module(''myApp'', []);app.config(function($controllerProvider) { $appControllerProvider = $controllerProvider; //cache this so that we can lazy load controllers});app.controller(''myCtrl'', function($scope) { $scope.sections = [''top'',''mid'',''bottom'']; //html files to load (top.html, etc) $scope.loadedSections = [$scope.sections[0]]; //loaded html files});app.directive(''scrollLoad'', function($compile) { return { restrict: ''A'', link: function(scope, element, attrs) { var to = scope[attrs.scrollLoadTo]; //$scope.loadedSections var from = scope[attrs.scrollLoadFrom]; //$scope.sections $window = angular.element(window); $window.bind(''scroll'', function(event) { var scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0; var scrollPos = scrollTop + document.documentElement.clientHeight; var elemBottom = element[0].offsetTop + element.height(); if (scrollPos >= elemBottom) { //scrolled to bottom of scrollLoad element $window.unbind(event); //this listener is no longer needed. if (to.length < from.length) { //if there are still elements to load //use $apply because we''re in the window event context scope.$apply(to.push(from[to.length])); //add next section } } }); } };});app.factory(''myService'', function($http, $q) { return { getController: function(fileName) { return $http.get(fileName+''.js'').then(function(response) { return response.data; }); } }});app.directive(''lazy'', function(myService, $compile, $q) { /* I store the directive in a variable then return it later * so that I can abstract directive logic into other functions below */ var directiveReturn = { restrict: ''A'', link: function(scope, element, attrs) { var loadName = scope.$eval(attrs.lazy); //this is straightforward - see the "addScript" function for explanation myService.getController(loadName).then(function(js) { return addScript(loadName, js, scope); }).then(function() { //the controller has been lazy loaded into angular //now use "ng-include" to lazy load the view. var ngInc = angular.element(''<span></span>'') .attr(''ng-include'', "''"+loadName+".html''") .attr(''ng-controller'', loadName+''Ctrl''); element.append(ngInc); $compile(ngInc)(scope); }); } //link }; //directive return /* * This is the magic. */ var scriptPromises = {}; function addScript(loadName, js, scope) { if (!scriptPromises[loadName]) { //if this controller hasn''t already been loaded var deferred = $q.defer(); //cache promise (which caches the controller when resolved) scriptPromises[loadName] = deferred.promise; //inject controller into a script tag var script = document.createElement(''script''); script.src = ''data:text/javascript,'' + encodeURI(js); script.onload = function() { //this is how you lazy load a controller $appControllerProvider.register(loadName, window[loadName+''Ctrl'']); //now that the controller is registered with angular, resolve the promise //then, it is safe to add markup that uses this controller with ng-controller scope.$apply(deferred.resolve()); }; //when this script loads, the controller will be registered and promise is resolved document.body.appendChild(script); return deferred.promise; } else { //controller already loaded return scriptPromises[loadName]; //use cached controller } } return directiveReturn;});
Angular 4延迟加载参数
这是我的懒加载路线:
{ path: 'venue/:name/:id',loadChildren: () => System.import('../containers/activity-detail/activity-detail.module').then((file: any) => { return file.default; }) },
我想路由到这个’activity-detail.module’,然后使用“:name”和“id:”参数加载详细信息.
加载的模块有自己的路由文件.
export const VenueDetailRoutes: Route[] = [ { path: '',redirectTo: 'venue/:name/:id',pathMatch: 'full' },{ path: 'venue/:name/:id',component: VenueDetailComponent,data: { shouldDetach: true,// Route will be resused. See CustomresuseStrategy. title: null } },{ path: '**',redirectTo: '/' } ];
似乎没有第一个默认对象没有任何作用.我收到错误:
{ path: '',pathMatch: 'full' },TypeError: Cannot read property 'path' of null
使用默认对象我得到错误:
Error: Cannot redirect to 'venue/:name/:id'. Cannot find ':name'.
任何帮助将不胜感激.
解决方法
{ path: '',
它无法匹配带参数的路径的“空”路径.
延迟加载路由的语法比我的复杂得多.我看起来像这样:
{ path: 'movies',loadChildren: './movies/movie.module#MovieModule' },
请注意,“父”路由(此示例中为“电影”)在延迟加载的路由中定义,并且不会在加载的模块路由中重复.
例如:
RouterModule.forChild([ { path: '',component: MovieListComponent },{ path: 'search',component: MovieSearchComponent },{ path: ':id',component: MovieDetailComponent } ])
在你的情况下,我认为加载模块的路由应该如下所示:
export const VenueDetailRoutes: Route[] = [ { path: ':name/:id',// Route will be resused. See CustomresuseStrategy. title: null } } ];
(尽管您可能需要考虑在基本路由工作之前不要使用自定义重用策略.)
angular – 在模块延迟加载时显示加载动画?
{ path: '',component: HomeComponent },{ path: 'profile',loadChildren: './profile/profile.module#ProfileModule' },{ path: '**',redirectTo: '',pathMatch: 'full' }
但是,我的配置文件模块加载需要大约1.5到2秒,这很慢.我想在我的模块加载时显示某种加载动画,无论如何都要这样做吗?我试过这样做:
app.component.html
<!-- other stuff ... --> <router-outlet></router-outlet> <div> <div> <div></div> <div></div> <div></div> <div></div> <div></div> </div> </div>
在我的CSS里面,我有:
/* default .loading styles,.loading should be invisible,opacity: 0,z-index: -1 */ .loading { opacity: 0; transition: opacity .8s ease-in-out; position: fixed; height: 100%; width: 100%; top: 0; left: 0; background: #1B1B1B; color: white; z-index: -1; } /* .loading screen is visible when app is not bootstraped yet,.my-app is empty */ router-outlet:empty + .loading,router-outlet:empty + .spinner { opacity: 1; z-index: 100; } /* spinner animation css stuff */
但这似乎不起作用,无论如何在加载angular2模块时显示一些加载器?
什么时候是空的:
<router-outlet></router-outlet>
因此,当它被加载时,它将是:
<router-outlet></router-outlet> <your-code></your-code> // which is loaded afterward
所以router-outlet:空不应该做任何事情.
你可以说 :
router-outlet + .loading .spinner { opacity: 1; z-index: 100; }
angular – 如何在延迟加载的模块中提供服务,并将该服务限定为延迟加载的模块及其组件?
这里的’module-scoped’是指模块还是扩展为包含属于该模块的所有组件?
我问的原因是因为我有一个带有2个属于它的组件的延迟加载模块.我在模块中注册了一个服务,但由于某种原因,每个组件都获得了该服务的不同实例.
为了在我的延迟加载模块中提供LazyModuleService并将该服务限定为延迟加载的模块及其组件,我需要更改什么?请包含所需的任何其他文件.我试图做一个通用的例子来帮助其他可能找到这个问题的人.
延迟加载模块:
import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { Component1 } from './component1.component'; import { Component2 } from './component2.component'; import { LazyModuleService } from './lazy-module.service'; @NgModule({ imports: [ CommonModule,],declarations: [ Component1,Component2,}) export class LazyLoadedModule { }
解决方法
经过深入调查后,似乎问题与如何实现延迟加载有关.
在您的情况下,您的延迟加载模块实际上已路由到其中的两个不同组件 – 这两个组件都直接公开为Router.forChild路由.但是,因此,当您导航到每个组件时,会为每个组件添加一个单独的延迟加载模块提供程序实例.
由于您实际上希望延迟加载模块中的所有组件共享其提供的服务的相同实例,因此您需要创建一个“根”组件,然后让您的两个组件成为该根组件的子组件.
似乎在延迟加载时,模块中的提供程序将添加到模块根部的组件的注入器中.由于您有两个“根组件”,因此每个组件都有不同的服务实例.
解决方案是创建一个单个根组件,其注入器将接收延迟加载的服务,然后可以由任何子组件共享.
angular – 延迟加载的DLL
我一直在看下面的例子:https://gist.github.com/robertknight/058a194f45e77ff95fcd
该示例将bundle分离为DLL,但是通过脚本标记包含它.
有没有办法告诉Webpack有条件地包含来自另一个包的DLL?
我们的想法是将它用作延迟加载技术,只有在某些用户特定的运行时条件为真时才加载bundle.
对于上下文,我在Angular 2中这样做
解决方法
代码拆分会将您的捆绑包拆分成多个块,只有在需要时才会自动获取.
NOTE: Webpack 1 uses
require.ensure
and Webpack 2 usesSystem.import
to define ‘split points’.
关于在页面滚动上延迟加载Angular视图和控制器和页面滚动加载如何实现的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Angular 4延迟加载参数、angular – 在模块延迟加载时显示加载动画?、angular – 如何在延迟加载的模块中提供服务,并将该服务限定为延迟加载的模块及其组件?、angular – 延迟加载的DLL的相关信息,请在本站寻找。
本文标签: