【发布时间】:2015-07-09 04:38:05
【问题描述】:
我正在尝试构建一个在我的不同视图之间滑动的应用,使用下一个和上一个按钮来切换视图。无论出于何种原因,尽管在 $ng-Route documentation 中,$route.next 和 $route.previous 在我的 .run() 语句中都不起作用。
index.html:
<!doctype HTML>
...
<button ng-click="go(prevRoute);" class="slider-left">Left</button>
<button ng-click="go(nextRoute);" class="slider-right">Right</button>
...
app.js
angular.module('myApp'
...
.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
title: 'Home',
niceName: 'home',
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'main'
})
.when('/about', {
title: 'About Us',
niceName: 'about',
templateUrl: 'views/about.html',
controller: 'AboutCtrl',
controllerAs: 'about'
})
.when('/projects', {
title: 'Our Work',
niceName: 'projects',
templateUrl: 'views/projects.html',
controller: 'ProjectsCtrl',
controllerAs: 'projects'
})
.when('/contact', {
title: 'Contact',
niceName: 'contact',
templateUrl: 'views/contact.html',
controller: 'ContactCtrl',
controllerAs: 'contact'
})
.otherwise({
redirectTo: '/'
});
// use the HTML5 History API
$locationProvider.html5Mode(true);
})
.run(['$rootScope', '$route', function($rootScope, $route) {
$rootScope.$on('$routeChangeSuccess', function() {
//create variable to add current route title to pageTitle
$rootScope.pageTitle = $route.current.title;
//create functions for next and previous buttons
$rootScope.nextRoute = $route.next.niceName;
$rootScope.prevRoute = $route.previous.niceName;
});
}])
.controller('AppCtrl', function ($scope, $location) {
$scope.go = function ( path ) {
$location.path( path );
};
});
当我尝试这个时,我得到“找不到未定义的属性”。
【问题讨论】:
标签: javascript angularjs ngroute