I'm using UI-router in my app and I'd like to so a simple "scrollTo" to an anchor when the URL/state changes. I want to load new controller and template when state changes. I'm using ng-boilerplate.
Index file
<div class="collapse navbar-collapse" collapse="menuCollapsed" ng-click="collapse()">
<ul class="nav navbar-nav navbar-right nav-link">
<li ui-sref-active="active">
<a href ui-sref="home">
<i class="fa fa-home"></i>
Home
</a>
</li>
<li ui-sref-active="active">
<a href ui-sref="service">
<i class="fa fa-info-circle"></i>
Service
</a>
</li>
<li ui-sref-active="active">
<a href ui-sref="portfolio">
<i class="fa fa-briefcase"></i>
Portfolio
</a>
</li>
<li ui-sref-active="active">
<a href ui-sref="team">
<i class="fa fa-users"></i>
Team
</a>
</li>
<li ui-sref-active="active">
<a href ui-sref="testimonial">
<i class="fa fa-comments"></i>
Testimonial
</a>
</li>
<li ui-sref-active="active">
<a href ui-sref="contact">
<i class="fa fa-envelope"></i>
Contact
</a>
</li>
</ul>
</div>
</div>
</div>
<div ui-view="main">
</div>
Home template and js
<div class="banner">
<div class="intro-body">
<h1>CREATIVE DIGITAL<br> SOLOUTIONS</h1>
<p>Proin iaculis consequat sem cure.</p>
<a href ui-sref="portfolio" class="btn btn-success">VIEW PORTFOLIO</a>
</div>
</div>
angular.module( 'ngBoilerplate.home', [
'ui.router',
'plusOne'
])
/**
* Each section or module of the site can also have its own routes. AngularJS
* will handle ensuring they are all available at run-time, but splitting it
* this way makes each module more "self-contained".
*/
.config(function config( $stateProvider ) {
$stateProvider.state( 'home', {
url: '/home',
views: {
"main": {
controller: 'HomeCtrl',
templateUrl: 'home/home.tpl.html'
}
},
data:{ pageTitle: 'Home' }
});
})
/**
* And of course we define a controller for our route.
*/
.controller( 'HomeCtrl', function HomeController( $scope ) {
})
;
Service Template & Js
<div class="container" id="service">
<div class="service-intro">
<h2>Why to choose company?</h2>
</div>
</div>
angular.module( 'service', [
'ui.router'
])
.config(function config( $stateProvider ) {
$stateProvider.state( 'service', {
url: '/service',
views: {
"main": {
controller: 'ServiceCtrl',
templateUrl: 'service/service.tpl.html'
}
},
data:{ pageTitle: 'service' }
});
})
.controller( 'ServiceCtrl', function HomeController( $scope ) {
})
;
APP JS
angular.module( 'ngBoilerplate', [
'templates-app',
'templates-common',
'ngBoilerplate.home',
'ngBoilerplate.about',
'service',
'portfolio',
'team',
'testimonial',
'contact-us',
'ui.router',
'ngAnimate'
])
.config( function myAppConfig ( $stateProvider, $urlRouterProvider ) {
$urlRouterProvider.otherwise( '/home' );
})
.run( function run () {
})
.controller( 'AppCtrl', function AppCtrl ( $scope, $location,$rootScope ) {
$rootScope.$on('$stateChangeStart', function () {
$scope.slide = $scope.slide || 'slide-left';
});
$scope.collapse = function() {
$scope.menuCollapsed = true;
};
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
if ( angular.isDefined( toState.data.pageTitle ) ) {
$scope.pageTitle = toState.data.pageTitle + ' | Teknuk' ;
}
});
})
;
So, when you enter the page the URL would be domain.com/home
When you click the first button I'd like my controller code to change the URL to domain.com/#/service and scroll down to the "service" div and controller + template updated.
Ideally, when the user hits the back button it would revert to the first URL and scroll back up to home.
Anybody know how to do this?
.config()function and call a.scrollTo()based on each states custom propertydata: {}on each state's change? Provide me a live example and I'll try it for you