我正在开发 Angular.js 应用程序。它有 7 个“页面”,组成了有关反向链接的报告。主网址看起来像 http://www.site.com/report/#因此“/”下面的第一条路由指向该 URL。
我的问题是我想在 url 中包含报告 ID。所以像这样的事情。 http://www.site.com/report/#/:reportId或http://www.site.com/report/#/99DF82A023FCE3515BE9A18F00908939F5A29D14 .
我需要访问该 ID 并以某种方式为每个页面存储它。依此类推http://www.site.com/report/#/live我仍然需要访问 reportID。
我确信这是一件简单的事情,但我是一个 Angular 菜鸟。有人可以向我指出这方面的教程吗?这是我到目前为止所拥有的。
'use strict';
// Declare app level module which depends on filters, and services
var ultModule = angular.module('ultimate-report', ['ngRoute', 'ngCookies', 'ngTable', 'ultimate.filters','ultimate.services','ultimate.directives','myApp.controllers', 'ultimate.config']);
ultModule.config(['$routeProvider', function($routeProvider, $routeParams) {
$routeProvider
.when('/', {templateUrl: '../partials/report/about.html', controller: 'CtrlReport'})
.when('/live', {templateUrl: '../partials/report/links-live.html', controller: 'CtrlLinksLive'})
.when('/dead', {templateUrl: '../partials/report/links-dead.html', controller: 'CtrlReport'})
.when('/selected', {templateUrl: '../partials/report/links-selected.html', controller: 'CtrlReport'})
.when('/patterns', {templateUrl: '../partials/report/patterns.html', controller: 'CtrlReport'})
.when('/exports', {templateUrl: '../partials/report/export.html', controller: 'CtrlReport'})
.when('/help', {templateUrl: '../partials/report/help.html', controller: 'CtrlReport'})
.otherwise({redirectTo: '/'});
}]);
最佳答案
那么您真的希望其他网址(例如 http://www.site.com/report/#/live )中不包含报告 ID 吗?如果每个地址都有参数(例如 http://www.site.com/report/#/12334223287/live ),那么会更容易/更短。
尽管如此,我还是会创建自己的服务(“ReportID”)来存储报告 ID。然后包括 $routeParams和 ReportID(您的服务)作为 Controller 中的依赖项。
app.controller('CtrlReport', ['$routeParams', 'ReportID', '$scope'],
function($routeParams, ReportID, $scope){
ReportID.id = $routeParams.reportID;
})
并设置您的路线
.when('/report/:reportID', ...)
关于angularjs - AngularJS 中的路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20708189/