javascript - 如何在单页应用程序中将数据从一个页面传递到另一个页面

标签 javascript jquery angularjs angular-ui-router

如何在 Angular 单页应用程序中将数据或值从一个页面传递到另一个页面。这是我第一次将 Angular/TypeScript 和 JavaScript 作为一个整体来使用。

我的目标是将userid传递到编辑用户页面。 我正在使用

 "angular": "^1.7.2",
 "angular-route": "^1.7.2",

我当前的结构是这样的 在 route.js

app.config(function($routeProvider){
  $routeProvider
  .when('/',{
    templateUrl:'home.html'
  })
  .when('/user',{
    templateUrl:'showUsers.html'
  })
.when('/user-edit/:userId',{
  templateUrl:'editUser.html'
})
})

showUsers.html

<a href="#!user-edit/92">User Edit screen</a>

现在我想做的是在用户编辑屏幕的 jquery 脚本中捕获 userId,以便我可以获得用户数据。

我该怎么做?

我正在关注

  1. https://www.w3schools.com/angular/angular_routing.asp
  2. https://docs.angularjs.org/tutorial

我还关注了一些关于堆栈溢出的问题

How do I pass data to Angular routed components? 但这对我没有帮助。

最佳答案

使用$routeParams服务:$routeParams服务允许我们检索路由参数。

var module = angular.module("myApp", ['ngRoute']);
module.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/route1/:param1/:param2', {
            templateUrl: 'route1.html',
            controller: 'RoutingController'
        })
        .when('/route2/:param1/:param2', {
            templateUrl: 'route2.html',
            controller: 'RoutingController'
        })
        .otherwise({
            redirectTo: '/route1/default-book/default-page'
        });
}]);
module.controller("RoutingController", function ($scope, $routeParams, $location) {
    // Using $routeParams
    $scope.param1 = $routeParams.param1;
    $scope.param2 = $routeParams.param2;
});

关于javascript - 如何在单页应用程序中将数据从一个页面传递到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51188609/

相关文章:

javascript 中的 php

javascript - 如何修复来自 Google Drive API 的 'The resource body includes fields which are not directly writable' 警告?

javascript - cfselect 绑定(bind)和 jquery 事件冲突

javascript - Angular 未定义 Gulp.js

javascript - 在 Controller angularJS中注入(inject)工厂

javascript - Laravel 5.5 MethodNotAllowedHttpException ajax

javascript - 如何使用名称属性克隆新的字段组并增加名称属性

jquery 和 bootstrap 导航栏在 Angular 7 中折叠或展开子菜单的布局中不起作用

javascript - 在 jQuery Mobile 中打开弹出窗口时如何避免上下滚动

angularjs - BackboneJS 和 AngularJS 排序稳定吗?