I have a project for which I need 2 layouts. One which is basically 1 column (kind of as a landing page) and one which has a menu at the top, and is basically the same 1 column layout.
I've set up 2 layout HTMLs, which I'm using in my states, but the problem is that every time I switch from one state to the other, the whole HTML in the top-most ui-view (the one in the body) is changed.
Here's my setup:
Routes
.state('root', {
url: '',
abstract: true,
templateUrl: 'app/layouts/logged-in.html'
})
.state('root.homepage', {
url: '/',
templateUrl: 'app/main/main.html',
controller: 'MainController',
controllerAs: 'main'
})
.state('candidate', {
url: '/candidate',
abstract: true,
templateUrl: 'app/layouts/logged-in.html'
})
.state('candidate.profile', {
url: '/profile',
templateUrl: 'app/candidate/profile.html',
controller: 'CandidateProfileController',
controllerAs: 'profile'
})
index.html
<body>
<div ui-view></div>
</body>
logged-in.html
<div>
<my-navbar></my-navbar>
</div>
<div ui-view></div>
logged-out.html
<div ui-view></div>
The problem is that when switching between root.homepage and candidate.profile, my-navbar gets removed and then re-appended, which the user can briefly see (until the new HTML loads). I'd understand if one of the states would have the logged-out.html layout and the other the logged-in.html layout, but we're talking about the same file, and I'm looking to update only the ui-view from inside logged-in.html file.
Might be better to create an abstract state for the logged-in (including the navbar) and then inside introduce div for placing named view mainContent. And configure all your states to extend logged-in state, with defining views:{'mainContent': {controller:... , templateUrl: ... } }
Related
I'm trying to use the same template for 2 different views. My set up is this
$stateProvider.state('me', {
url: '/me',
templateUrl: 'partial/profile/profile.html',
controller: 'ProfileController'
});
$stateProvider.state('myteam', {
url: '/myteam',
templateUrl: 'partial/myteam/myteam.html',
controller: 'MyteamController'
});
$stateProvider.state('myteam.teamMember', {
url: '/:username',
templateUrl: 'partial/profile/profile.html',
controller: 'ProfileController'
});
However whenever I try and access myteam.teamMember, the URL changes but the view doesn't change. Anyone have any advice?
When you redirect to child state it looks for ui-view(it can be named view) on current state HTML, and load the state template in it. Basically you are redirecting to child state of myteam, so to get the changes on view you should have ui-view somewhere inside partial/myteam/myteam.html HTML file.
partial/myteam/myteam.html
<div>
MY HTML
......
.......
<div ui-view></div>
</div>
I've been working with Angular for a year or 2 now, but this is my first project using ui-router. I'm running into a few issues with views and sub-views. The app is a standard left-side menu bar, with the views on the right changing depending on what's clicked in the menu bar.
On index.html
<body>
<div ui-view></div>
</body>
In the config.js file, which defines the routes
.state("dashboard", {
url: "/dashboard",
templateUrl: "components/dashboard/dashboard.html",
data: {
pageTitle: "Dashboard",
requiresLogin: false
}
})
.state("dashboard.welcome", {
url: "/welcome",
templateUrl: "components/welcome/welcome.html",
data: {
pageTitle: "Welcome",
requiresLogin: false
}
})
In the dashboard.html file
<div class="dashboard">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-8">
<div ui-view>
The /dashboard path loads correctly, and will load the left-side navigation bar with a blank right side. But changing the state to dashboard.welcome (/welcome) will not load the welcome.html template.
Whenever working with ui-router you need to understand that the concept of states is different from routes. When you define a sub-state, its defined relative to its parent state. In your scenario dashboard.welcome is defined as a child state of dashboard. The routes to substate is relative to the parent and is {parent url}/{child url}. Hence you should use either of the below 2 to route to that state:
Using $state.go change the state by specifying state name
$state.go('dashboard.welcome');
Using $location.path change the route by specifying url
$location.path('/dashboard/welcome');
It sounds like you want links to /welcome to be for state dashboard.welcome. Here is a plunker showing how this can be done. I show two sets of dashboard and welcome states. The first set of states (dashboard & welcome) shows that /dashboard/welcome will bring you to the dashboard.welcome state.
The second set (dashboard2 & welcome2) shows that /welcome will go to state dashboard2.welcome2. I believe this is what you were looking for.
If you hover over the links you can see where they will take you.
https://plnkr.co/edit/AVKPFa?p=info
Nested routes in ui-router get nested urls. I would however recommend using named-views for this kind of structure. You can find more info about it here:
https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views
The gist of it is: you can specify a named component (ui-view) for your left menu navigation and another one for content, which gives you much more control down the line, because named components can be overwritten in child states or they can keep the default template, depending on your needs.
Example:
.state('root', {
url: '',
abstract: true,
views: {
'header': {
templateUrl: 'templates/partials/header.html',
controller: 'headerCtrl'
},
'logo': {
templateUrl: 'templates/partials/logoView.html'
},
'footer':{
templateUrl: 'templates/partials/footer.html',
controller: 'footerCtrl'
}
}
})
.state('root.login', {
url: '/login',
views: {
'header#': {
template: ''
},
'container#': {
templateUrl: 'templates/login.html',
controller: 'loginController'
}
}
})
.state('root.report', {
url: '/report',
views: {
'container#': {
templateUrl: 'templates/eu_dashboard.html',
controller: 'reportController'
}
}
})
And in your index.html:
<div ui-view="logo"></div>
<div ui-view="header"></div>
<div id="mainView" ui-view="container"></div>
<div ui-view="footer"></div>
I'm building an angular application using ui-router, and there's one thing I can't figure out.
There's a sidebar on the left, and a content area. Both are controlled by own controllers. When I choose an item in the sidebar, the content area shall be updated, but the sidebar must remain its state. What it does instead is that the sidebar reloads as well when selected an item.
app.coffee:
$urlRouterProvider.otherwise "/items/near/map"
$stateProvider
.state('items',
url: '/items'
abstract: true
templateUrl: "items.html"
)
.state('items.near',
url: '/near'
abstract: true
views:
'sidebar#items':
templateUrl: 'items-near-list.html'
controller: 'ItemsNearListCtrl'
)
.state('items.near.map',
url: '/map'
views:
'content#items':
templateUrl: 'items-near-map.html'
controller: 'ItemsNearMapCtrl'
)
.state('items.near.detail',
url: '/detail/:id'
views:
'content#items':
templateUrl: 'item-detail.html'
controller: 'ItemsNearDetailCtrl'
)
items.html:
<div>
<div ui-view="sidebar"></div>
<div ui-view="content"></div>
</div>
A probably related problem it that if I enter a detail view state directly (/items/near/detail/x/), the sidebar does not even load anything. Any idea what I'm doing wrong?
Hmm I never used the absolute # syntax for the view names but after looking into the docs it seems this may be the problem.
Please try without the #items or use the full state/view-combination like sidebar#items.near, content#items.near.map and content#items.near.detail.
I am new to angularjs, so please consider my mistakes.
The scenario is, I have an index.html page and login.html page, index and login will have different layout thus different header footer and all.
All other pages expect login will share same layout as index.html
Now, how can I navigate to login.html using anuglar ui router.
myApp.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider.state('home', {
url: '/',
templateUrl: 'app/views/site/home.html',
controller: 'homeCtrl'
}).state('login', {
url: '/login',
templateUrl: 'app/views/site/login.html'
})
});
This does not work as it loads the content of login into index keeping header and footer same of index. I know the state is supposed to work that way.
Now, is there any way I can just call a completely new view using ui.router.
I really appreciate your help.
Thanks
You could probably set up multiple named views (https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views).
This would allow you to make the header and footer into separate views that you can swap in and out. I don't think there is a way to completely swap out an entire page, though.
<body>
<header ui-view="header"></header>
<main ui-view="content"></main>
<footer ui-view="footer"></footer>
</body>
.state('login', {
views: {
'header': { ... templates and/or controllers ... },
'content': { ... templates and/or controllers ... },
'footer': { ... templates and/or controllers ... },
}
})
I achieve this by making my entire layout into templates. Essentially, my index.html is nothing more than this:
<html ng-app="appName" ng-strict-di>
<head>
<title></title>
</head>
<body>
<div class="ng-cloak" ng-cloak ui-view></div>
</body>
</html>
Then I use abstract states to create template zones. For instance, my auth states:
$stateProvider
.state('auth', {
abstract: true,
url: '/auth',
template: '<div ui-view></div>'
})
.state('auth.login', {
url: '/login',
templateUrl: 'auth/login.html',
controller: [ loginController],
controllerAs: 'vm'
})
.state('auth.logout', {
url: '/logout',
resolve: {
logout: [ 'api', function (api) {
return api.getLogout().$promise;
}]
},
controller: [ logoutController],
controllerAs: 'vm'
});
This causes the auth/login route to render directly into the main ui-view where it can have it's own unique layout. Similarly, I may have an abstract core state that sets a common scope and template for the core parts of my app, which all share a common template structure.
If there are re-used elements that touch both the core and auth sections, then I use ng-include to bring in those individual partials.
So, it seems I am not getting ui-router, after all.
Here is the broken example:
http://plnkr.co/edit/WgDqTzE3TJjrCZ2pxg5T?p=preview
The actual file structure is:
app/
app.js
index.html
main/
main.html
header/
header.html
footer/
footer.html
sections/
content1/
content1.html
content2/
...
index.html has a simple <div ui-view></div>
main.html has:
<div ui-view="header"></div>
<div ui-view></div>
<div ui-view="footer"></div>
header.html, footer.html, content1.html, ... have actual content.
app.js has:
$stateProvider
.state("app", {
url: "",
abstarct: true,
templateUrl: "main.html"
})
.state("app.main", {
url: "",
abstarct: true,
views: {
"header": {
templateUrl: "header.html"
},
"footer":{
templateUrl: "footer.html"
}
}
})
.state("app.main.content1", {
url: "/",
templateUrl: "content1.html"
});
So, I thought this meant going to "/" would show me header, footer, and automatically insert content in the unnamed ui-view.
It does not. What am I doing wrong?
To make it quickly working (not as intended) I've just added an anchor into the footer and now it is working
<div>
im footer
<div ui-view=""></div>
</div>
Why? because our state def is like this:
.state("app.main.content1", {
url: "/",
templateUrl: "content1.html"
});
Which means:
do search for unnamed view in my parent
The footer view is a parent of the state "app.main.content1", so this way we can target it.
There are other options as well.
Here is what was most likely intended: , state defintion is changed:
.state("app.main.content1", {
url: "/",
views: {
"#app": {
templateUrl: "content1.html"
}
}
So, now we target the app state, its view anchor <div ui-view="">, by the absolute naming. See:
View Names - Relative vs. Absolute Names
Behind the scenes, every view gets assigned an absolute name that follows a scheme of viewname#statename, where viewname is the name used in the view directive and state name is the state's absolute name, e.g. contact.item. You can also choose to write your view names in the absolute syntax.
At the end, we can reach the same with even one abstract state and one child, check it here:
$stateProvider
.state("app", {
url: "",
abstarct: true,
views: {
"" : {
templateUrl: "main.html",
},
"header#app": {
templateUrl: "header.html"
},
"footer#app":{
templateUrl: "footer.html"
}
}
})
.state("app.content1", {
url: "/",
templateUrl: "content1.html",
});
Now, the all UI-Router magic is happening in the root state def. We firstly target root (index.html) unnmaed view, to inject the main.html. Next - othe views target this state itself (the main.html) via "header#app" - absolute naming
That would be the most suitable way... working example