2015-05-22 48 views
1

HTML:Angular中的简单哈希爆炸示例。为什么它不起作用?

<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://jsfiddle.net/sprhgucv/

请能有人让我知道为什么我没有看到我的联系之间单击任何记录?

我只找到了一个很好的例子,这就是我试图实现,但无济于事:http://fdietz.github.io/recipes-with-angular-js//urls-routing-and-partials/client-side-routing-with-hashbang-urls.html

+0

您需要包含angular-route.js文件。 – jcubic

+0

我在本地完成了,但我没有在小提琴中。请你可以告诉我日志工作的小提琴 – alexrogins

+1

你还需要ng-app。我试图修复小提琴但得到错误。 – jcubic

回答

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' 
}); 

});

相关问题