[英]Angular ui-router loading view twice
I'm using angular ui-router in my angular application. 我在我的角度应用程序中使用角度ui路由器。 For the routing part I've written
对于我写的路由部分
var routerApp = angular.module('routerApp', ['ui.router']);
routerApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
// HOME STATES AND NESTED VIEWS ========================================
.state('home', {
url: '/home',
templateUrl: 'partial-home.html',
controller : function($scope,$state){
$scope.$on('$viewContentLoaded', function(){
console.log("home content loaded",$state.current);
})
}
})
// nested list with custom controller
.state('home.list', {
url: '/list',
template: 'I am using a list.'
})
// nested list with just some random string data
.state('home.paragraph', {
url: '/paragraph',
template: 'I could sure use a drink right now.'
})
});
In partial-home.html I've wriiten:- 在partial-home.html中,我写过:-
<div class="jumbotron text-center">
<h1>The Homey Page</h1>
<p>This page demonstrates <span class="text-danger">nested</span> views.</p>
<a ui-sref=".list" class="btn btn-primary">List</a>
<a ui-sref=".paragraph" class="btn btn-danger">Paragraph</a>
</div>
<div ui-view></div>
Now when I'm executing this app in browser, if I open the the route 现在,当我在浏览器中执行此应用时,如果我打开路线
/home
The browser console is showing "home content loaded" once, and then if I click on the list link, the addressbar changes to 浏览器控制台一次显示“已加载家庭内容”,然后如果我单击列表链接,则地址栏将更改为
/home/list
And then also the console is shown once. 然后控制台也会显示一次。
But if I refresh the browser tab at that time (when the route is home/list), the console is shown twice. 但是,如果我当时(当路由为“主页/列表”时)刷新浏览器选项卡,则控制台将显示两次。 Please help me why this thing happening.
请帮我为什么这件事发生。
These are nested views. 这些是嵌套视图。 The reason why there are two messages in your console after reload is: When you start at /home, the home state is loaded.
重新加载后控制台中出现两条消息的原因是:当您从/ home启动时,将加载home状态。 When you go to /home/list over there, only home.list need to be loaded since the home state is already loaded.
当您转到那里的/ home / list时,由于home状态已经加载,因此只需要加载home.list。 When you reload there are two states that need to be loaded: home and home.list.
重新加载时,需要加载两种状态:home和home.list。
Edit: i dont have enough reputation so i cant comment. 编辑:我没有足够的声誉,所以我不能发表评论。 i guess that you use ui-view inside home template which causes child and parent to share the same scope.
我猜您在家庭模板中使用ui-view会导致子级和父级共享同一范围。 u are listening to both child's and parent's scope for the event.
您正在听孩子和父母的活动范围。 so it is natural to be called twice
所以被叫两次是很自然的
because home.list is a child state of home and when you reload page for home.list it first initializes view of home controller than view of list controller so $viewContentLoaded is called twice 因为home.list是home的子状态,并且当您重新加载home.list的页面时,它首先初始化home控制器的视图而不是list控制器的视图,因此$ viewContentLoaded被调用两次
i guess that you use ui-view inside home template which causes child and parent to share the same scope. 我猜您在家庭模板中使用ui-view会导致子级和父级共享同一范围。 u are listening to both child's and parent's scope for the event.
您正在听孩子和父母的活动范围。 so it is natural to be called twice
所以被叫两次是很自然的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.