简体   繁体   English

前端应用程序的角路由

[英]Angular routing for front end app

I know this is a very basic question, but can't seem to get everything hooked up right - I'm trying to get a handle on front end routing, using an Angular/Sinatra app. 我知道这是一个非常基本的问题,但似乎无法正确解决所有问题-我正在尝试使用Angular / Sinatra应用程序掌握前端路由。 My app isn't hitting the right page, and I'm at a loss to why, as I'm having some trouble wrapping my head around the way routes work if you're not using a database. 我的应用程序没有到达正确的页面,我不知道为什么,因为如果您不使用数据库,那么我在解决路由工作方式方面会遇到一些麻烦。

In my main page, "display.html", I have some text, then a set of D3 graphs with hrefs: 在我的主页“ display.html”中,有一些文本,然后是一组带有href的D3图:

<a href="/app/{{appname}}">
  <div ng-cloak class="nvd3-bullet-chart2" id="chart" data="subsetVals" showXAxis="true" showYAxis="true">
  </div>
</a>

The charts are populated by looping through an NVD3 function for each, rather than using ng-repeat (not sure how to do that given issues with asynchronicity). 通过为每个循环遍历NVD3函数来填充图表,而不是使用ng-repeat(不确定给定的异步问题如何执行)。

In my displayCtrl, I define $scope.appname like this: 在我的displayCtrl中,我这样定义$ scope.appname:

$scope.appname = ''

var getGraphs = function(dataset){
    var stats = {
        app: dataset[0].App
    }
    $scope.appname = stats.app;
    $scope.appname = $routeParams.appname;
}

I have my angular routes set up like this: 我有这样的角度路线设置:

app.config(function($routeProvider) {
    $routeProvider

    // route for the display page
        .when('/', {
            templateUrl: '/views/display.html',
        })
        .when('/app/:appname', {
            templateUrl: 'views/details.html',
        });
});

When I click on the href for the charts, I'm getting an error that the page wasn't found at '/app/', which to me indicates that the :appname is never being picked up. 当我单击图表的href时,出现一个错误,即在'/ app /'上找不到该页面,这对我来说表明:appname从未被使用。 So where should this be defined? 那么应该在哪里定义呢?

I'm happy to add more detail and code if needed. 如果需要,我很乐意添加更多详细信息和代码。 Thanks! 谢谢!

Try adding a hash to your href. 尝试将哈希添加到href中。

<a href="#/app/{{appname}}">

If you dont want hashes in your urls, you will have to setup the html5mode in your route settings. 如果您不想在网址中使用哈希,则必须在路由设置中设置html5mode。

$locationProvider.html5Mode(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM