angular-页面跳转传递参数

页面1:传递参数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../angular.js"></script>
</head>
<body ng-app="myAppGoTo1" ng-controller="myCtrlGoTo1">
<button ng-click="goTo()">跳转到page2,并携带参数</button>

<script>
    var app = angular.module("myAppGoTo1", []);

    app.controller("myCtrlGoTo1", function ($scope) {
        $scope.goTo = function () {
            var id = 100;
            var name = "zhangsan";
            var person = {firstname: "John", lastname: "Doe", age: 50, eyecolor: "blue"};//对象
            location.href = "page2.html?id=" + id + "&name=" + name + "&person=" + angular.toJson(person);
        }
    });
</script>
</body>
</html>

页面二:获取参数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../angular.js"></script>
</head>
<body ng-app="myAppGoTo2" ng-controller="myCtrlGoTo2">
<button ng-click="get1()">$location.search()获取page1传过来的参数</button>
<button ng-click="get2()">location.search:replace获取page1传过来的参数</button>
<button ng-click="get3()">location.search:subStr获取page1传过来的参数</button>

<script>
    var app = angular.module("myAppGoTo2", []);

    //$location.search()需要此配置
    app.config(['$locationProvider', function ($locationProvider) {
        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        });
    }]);

    app.controller("myCtrlGoTo2", function ($scope, $location) {
        $scope.get1 = function () {
            console.log("$location.search()===" + $location.search());//[object Object]
            console.log("$location.search()类型===" + typeof $location.search());//object
            if ($location.search().id) {
                console.log("id======" + $location.search().id);
                console.log("id类型======" + typeof $location.search().id);//string
            }
            if ($location.search().name) {
                console.log("name======" + $location.search().name);
                console.log("name类型======" + typeof $location.search().name);//string
            }
            if ($location.search().person) {
                console.log("person======" + $location.search().person);
                console.log("person类型======" + typeof $location.search().person);//string
                //$location.search()获取的都是字符串,如果想得到对象需要再次转换
                console.log("person======" + angular.fromJson($location.search().person));
                console.log("person类型======" + typeof angular.fromJson($location.search().person));
            }
        };

        $scope.get2 = function () {
            var searchStr = location.search;
            var searchArr = searchStr.split("&");
            var id = searchArr[0].replace("?id=", "");//string
            var name = searchArr[1].replace("name=", "");//string
            var person = decodeURI(searchArr[2].replace("person=", ""));//decodeURI解码,string

            console.log("location.search====" + searchStr);//?id=100&name=zhangsan
            console.log("location.search类型====" + typeof searchStr);//string
            console.log("id====" + id);
            console.log("id类型====" + typeof id);
            console.log("name====" + name);
            console.log("name类型====" + typeof name);
            console.log("person====" + person);
            console.log("person类型====" + typeof person);
            //location.search获取的都是字符串,如果想得到对象需要再次转换
            console.log("person====" + angular.fromJson(person));
            console.log("person类型====" + typeof angular.fromJson(person));
        };

        $scope.get3 = function () {
            var searchStr = location.search;
            console.log("location.search====" + searchStr);//?id=100&name=zhangsan
            console.log("location.search类型====" + typeof searchStr);//string
            var paramsStr = searchStr.substr(searchStr.indexOf("?") + 1);//先去掉?
            var paramsArr = paramsStr.split("&");//再以&分割成数组
            var id = paramsArr[0].substr(paramsArr[0].indexOf("=") + 1);
            var name = paramsArr[1].substr(paramsArr[1].indexOf("=") + 1);
            var person = decodeURI(paramsArr[2].substr(paramsArr[2].indexOf("=") + 1));//decodeURI解码,string

            console.log("location.search====" + searchStr);//?id=100&name=zhangsan
            console.log("location.search类型====" + typeof searchStr);//string
            console.log("id====" + id);
            console.log("id类型====" + typeof id);
            console.log("name====" + name);
            console.log("name类型====" + typeof name);
            console.log("person====" + person);
            console.log("person类型====" + typeof person);
            //location.search获取的都是字符串,如果想得到对象需要再次转换
            console.log("person====" + angular.fromJson(person));
            console.log("person类型====" + typeof angular.fromJson(person));
        }
    });
</script>
</body>
</html>

参考:

【更新】AngularJs $location获取url参数

Angular 中,可以使用 Angular 路由器来实现页面之间的跳转。首先,需要在 app.module.ts 文件中引入 RouterModule 模块,然后在 app-routing.module.ts 文件中定义路由规则。 例如,要实现从首页跳转到详情页,可以在 app-routing.module.ts 文件中定义如下路由规则: ```typescript import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { HomeComponent } from './home.component'; import { DetailComponent } from './detail.component'; const routes: Routes = [ { path: '', component: HomeComponent }, { path: 'detail/:id', component: DetailComponent }, ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } ``` 其中,`path` 表示页面的 URL 路径,`component` 表示对应的组件。 然后在 HomeComponent 中设置一个链接,点击链接跳转到详情页: ```html <a [routerLink]="['/detail', item.id]">查看详情</a> ``` 其中,`routerLink` 指令用于设置链接,`['/detail', item.id]` 表示跳转到详情页,并传递一个参数 `id`。 最后,在 DetailComponent 中可以通过 ActivatedRoute 服务获取传递过来的参数: ```typescript import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-detail', template: ` <h2>详情页</h2> <p>ID: {{ id }}</p> ` }) export class DetailComponent implements OnInit { id: number; constructor(private route: ActivatedRoute) { } ngOnInit() { this.id = +this.route.snapshot.paramMap.get('id'); } } ``` 其中,`ActivatedRoute` 服务用于获取当前路由参数,`snapshot.paramMap.get('id')` 表示获取参数 `id` 的值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值