HTML:
<a href="#!foo">foo</a>
<a href="#!bar">bar</a>
<div ng-view></div>
JS:
angular.module('theApp', ['ngRoute'])
.config(function($routeProvider, $locationProvider) {
$locationProvider.hashPrefix('!');
$routeProvider
.when('/foo', {
title: 'foo',
controller: 'foo'
})
.when('/bar', {
title: 'bar',
controller: 'bar'
})
})
.controller('foo', function(){
console.log('foo');
})
.controller('bar', function(){
console.log('GGGGG');
});
请有人让我知道为什么我在链接之间点击时看不到任何记录?
我只发现了一个很好的例子,这就是我试图实现但无济于事的结果:http://fdietz.github.io/recipes-with-angular-js//urls-routing-and-partials/client-side-routing-with-hashbang-urls.html
答案 0 :(得分:1)
是的,您需要包含角度路线。另外,如果没有提供某种模板,我无法让它工作,否则你的代码应该可行。这是一个有效的jsfiddle:http://jsfiddle.net/y93oxopg/
routingExample.config(function ($routeProvider, $locationProvider) {
$locationProvider.hashPrefix('!');
$routeProvider.
when('/home', {
template: '<div>HERE</div>',
controller: 'HomeController'
}).
when('/about', {
template: '<div>HERE2</div>',
controller: 'AboutController'
}).
otherwise({
redirectTo: '/home'
});
});