代码之家  ›  专栏  ›  技术社区  ›  user7747472

如何在单页应用程序中以角度将数据从一页传递到另一页

  •  0
  • user7747472  · 技术社区  · 7 年前

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

    我的目标是通过 userid edit user 第页。 我正在使用

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

    我现在的结构是这样的 在里面 路线JS

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

    在里面 显示用户.html

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

    现在我要做的是在用户编辑屏幕的jquery脚本中捕获用户ID,以便获取用户数据。

    我该怎么做?是吗?

    我在跟踪

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

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

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

    2 回复  |  直到 7 年前
        1
  •  2
  •   Chellappan வ    7 年前

    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;
    });
    
        2
  •  0
  •   JpG    7 年前

    /user-edit/:userId

    http://some-sitename.com#/user-edit/92
    

    $routeParams

    $routeParams.userId

    app.controller("EditController", function ($scope, $routeParams) {
        // Using $routeParams
        $scope.userId = $routeParams.userId;
    
         $scope.editMethod = function() {
             // inside this method use $scope.userId
          };
    
    });