angular路由传参

<!DOCTYPE html>
<html ng-app="myapp">
<head>
    <meta charset="UTF-8">
    <title>web站点入口页面</title>
    <link rel="stylesheet" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/font-awesome.css"/>
    <script src="js/angular.js"></script>
    <script src="js/angular-route.js"></script>
    <script>
        // 创建一个主模块 - 注意添加对ngRoute路由模块的依赖
        var myapp = angular.module("myapp", ["ngRoute"]);
        // 路由配置
        myapp.config(function ($routeProvider) {
            $routeProvider.when("/", {
                templateUrl: "pages/home.html",
                controller: "homeCtrl"}
            );
            $routeProvider.when("/about", {
                templateUrl: "pages/about.html",
                controller: "aboutCtrl"}
            );
            $routeProvider.when("/contact", {
                templateUrl: "pages/contact.html",
                controller: "contactCtrl"}
            );
            $routeProvider.when("/contact/:subject", {
                templateUrl: "pages/contact.html",
                controller: "contactCtrl"}
            );
            $routeProvider.otherwise({
                templateUrl: "pages/routeNotFound.html",
                controller: "notFoundCtrl"}
            );
        });

        // 注册多个控制器
        myapp.controller("homeCtrl", function ($scope) {
            $scope.message = "hello,this is home page";
        });
        myapp.controller("aboutCtrl", function ($scope) {
            $scope.message = "hello,this is about page";
        });
        myapp.controller("contactCtrl", function ($scope,$routeParams) {
            $scope.message = "hello,this is contact page";

            // angular会将传过来的url参数封装到一个对象中: {"subject":"b"}
            // 解析url参数
            var param = $routeParams["subject"];

            // 判断传过来的参数
            if(param == "a"){
                $scope.title = "我想提建议";
            }else if(param == "b"){
                $scope.title = "我想询价";
            }
        });
        myapp.controller("notFoundCtrl", function ($scope,$location) {
            $scope.message = "hello,this is Not Found File page";

            $scope.path = $location.path();   // 获取url中hash部分-路径
        });

        myapp.controller("myCtrl",function($scope){
            $scope.hello = "hello hello";
        });
    </script>
</head>
<body ng-controller="myCtrl">
<header>
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="#/">我的站点</a>
            </div>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#/"><i class="fa fa-home"></i>主页</a></li>
                <li><a href="#about"><i class="fa fa-shield"></i> 关于我们</a></li>
                <li><a href="#contact"><i class="fa fa-comment"></i> 联系我们</a></li>
            </ul>
        </div>

    </nav>
</header>

<div id="main">
    <!--子视图切换在这里-->
    <div ng-view></div>
</div>

</body>

</html>



<div class="jumbotron text-center">
    <h1>About Page</h1>
    <p>这里显示消息</p>
    <p>{{message}}</p>
    <p>{{hello}}</p>
    <div>
        <p>如果您想了解更多有关我们<a href="#contact/a">请告诉我们</a>.</p>
        <p>如果您想要一个免费报价,请致电我们,或者通过<a href="#contact/b">询价</a>.</p>
    </div>
</div>


<div class="jumbotron text-center">
    <h1>Home Page</h1>
    <p>这是Home页面</p>
    <p>{{message}}</p>
    <p>{{hello}}</p>
</div>



<div class="jumbotron text-center">
    <h1>这里显示消息</h1>
    <p>{{message}}</p>
    <p>{{hello}}</p>
    <form style="width:25%;margin:auto;" role="form">
        <div class="form-group">
            <input type="text" class="form-control"
                   id="subject" placeholder="Subject"
                   ng-model="title">
        </div>
        <div class="form-group">
            <textarea class="form-control" id="message" placeholder="Message"></textarea>
        </div>
        <button type="submit" class="btn btn-default">发送信息</button>
    </form>
</div>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值