Please help me with the nested ui-router routing. I will give a simple example.
There are 3 URLs:
/ main
/ main / apples
/ main / oranges
There is a navigation from two points: apples and oranges. It is present only on the pages / main / apples and / main / oranges and serves to switch between these pages. The div with this navigation should not restart when switching between these two sections.
angular.module('fruits', ['ui.router']); angular.module('fruits').config(function($stateProvider, $urlRouterProvider, $locationProvider){ $stateProvider.state({ name: 'main', url: '/main', templateUrl: 'templates/main.html' }); $stateProvider.state({ name: 'main.oranges', url: '/oranges', templateUrl: 'templates/oranges.html' }); $stateProvider.state({ name: 'main.apples', url: '/apples', templateUrl: 'templates/apples.html' }); $urlRouterProvider.otherwise('/'); $locationProvider.html5Mode({ enabled: true, requireBase: false }); });
main.html:
<!-- Заголовок и текст главной страницы, которые должны присутствовать только на главной странице --> <h1> Главная страница </h1> <span> Содержимое главной страницы </span> <!-- Навигация, которая должна присутствовать только на страницах /main/oranges и /main/apples и не перезагружаться при переходе между ними --> <div class="navigation"> <a ui-sref="main.oranges"> Яблоки </a> <a ui-sref="main.apples"> Апельсины </a> </div> <ui-view> </ui-view>
I need the pages of / main / oranges and / main / apples not to contain the contents of the main page, but navigation remains. Now on the / main / oranges and / main / apples pages both.
Thanks for the Grundy hint. Changed the code as follows:
main.html:
<div ui-view="fruits-main"></div> <div ui-view="nav"></div> <ui-view></ui-view>
app.js:
angular.module('fruits', ['ui.router']); angular.module('fruits').config(function($stateProvider, $urlRouterProvider, $locationProvider){ $stateProvider.state({ name: 'main', url: '/main', views: { '': { templateUrl: 'templates/main.html', }, 'fruits-main@main': { templateUrl: 'templates/fruits-main.html', } } }); $stateProvider.state({ name: 'main.apples', url: '/apples', views: { '': { templateUrl: 'templates/apples.html', }, 'nav': { templateUrl: 'templates/nav.html', }, 'fruits-main': { abstract: true, } } }); $stateProvider.state({ name: 'main.oranges', url: '/oranges', views: { '': { templateUrl: 'templates/oranges.html', }, 'nav': { templateUrl: 'templates/nav.html', }, 'fruits-main': { abstract: true, } } }); $urlRouterProvider.otherwise('/'); $locationProvider.html5Mode({ enabled: true, requireBase: false }); });
But not solved one problem. Navigation restarts every time you move from one section to another. How to be?
apples.html
<h1> Яблоки </h1> <p>Текст о яблоках</p>
nav.html
<!-- Навигация, которая должна присутствовать только на страницах /main/oranges и /main/apples и не перезагружаться при переходе между этими разделами --> <div class="navigation"> <a ui-sref="main.apples" ui-sref-active="active"> Яблоки </a> <a ui-sref="main.oranges" ui-sref-active="active"> Апельсины </a> <div class="animation"></div> </div>
fruits-main.html
<h1>Главная страница</h1> <p>Какой-то текст на главной странице</p>
css:
*{ margin: 0; padding: 0; } .active{ color: red !important; } @-webkit-keyframes go { from { left: 0px; } to { left: 500px; } } .animation{ width: 50px; height: 50px; background: green; position: relative; left: 0; -webkit-animation: go 1s infinite alternate; }