如何在 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,以便我可以获得用户数据。
我该怎么做?
我正在关注
我还关注了一些关于堆栈溢出的问题
最佳答案
使用$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/