gpt4 book ai didi

javascript - 第一次加载 Angular View 故障

转载 作者:行者123 更新时间:2023-11-28 18:59:42 25 4
gpt4 key购买 nike

当用户单击超链接并更改 ng-view 时,第一次需要更多时间,但从下一次开始,从一个 View 到另一个 View 的切换非常流畅,没有任何延迟。我在这里整理了一个示例 plunker:http://plnkr.co/edit/cINyIOpJBEAsa2pUk3tP?p=preview

来自那个骗子的 app.js 看起来像:

var sampleApp = angular.module('sampleApp', [
'ngRoute',
'ngAnimate'
]);

sampleApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/ShowOrder/:orderId', {
templateUrl: 'templates/show_order.html',
controller: 'ShowOrderController'
});
}
]);


sampleApp.controller('ShowOrderController', function($scope, $routeParams) {


$scope.order_id = $routeParams.orderId;
});

sampleApp.animation('.content',
function() {
return {
enter: function(element, done) {
var delay = $('.content').length === 2 ? 600 : 0; // if there are 2 .content (ngView) delay entrance, so the 1st can leave
$(element).css({
opacity: 0 // set the stage
});
$(element).delay(delay).animate({ // animate the opacity with delay if needed
opacity: 1
}, 600, done);
},
leave: function(element, done) {

$(element).css({
position: 'absolute', // use position absolute so the element won't jump down
opacity: 1 // set the stage
});
$(element).animate({
opacity: 0
}, 600, done);

}
}
}
)

在我的企业应用程序中,由于我所做的更多数据和变量初始化,这种初始延迟对用户来说更加明显。

有什么办法可以让它更流畅吗?

最佳答案

初始延迟是由路由模板请求引起的。可以通过提前缓存模板来消除它,

sampleApp.run(function ($http, $templateCache) {
$http.get('templates/show_order.html', { cache: $templateCache });
});

或者在构建阶段通过 grunt-angular-templates 连接模板或grunt-html2js .

关于javascript - 第一次加载 Angular View 故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32847758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com