使用angularjs的ui-view實現頁面切換、傳值的效果

更詳細的介紹請參考如下博客: https://blog.csdn.net/zcl_love_wx/article/details/52034193css

<!doctype html>
<html ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.6/angular.min.js"></script>
    <script src="https://cdn.bootcss.com/angular-ui-router/1.0.20/angular-ui-router.min.js"></script>
    <style>
        .active {
            font-size: 20px;
            color: green;
        }
    </style>
</head>
<body>
<ul>
    <li><a ui-sref="hello" ui-sref-active="active">hello</a></li>
    <li><a ui-sref="world({receivedId:1})" ui-sref-active="active">world</a></li>
</ul>
<div>
    <div ui-view></div>
</div>
<script>
    var app = angular.module("myApp", ["ui.router"]);
    app.config(["$stateProvider", "$urlRouterProvider",
        function ($stateProvider,$urlRouterProvider) {
            $urlRouterProvider.otherwise("/hello");
            $stateProvider.state('hello',{
                url: "/hello",
                template: "<h1>這是hello對應的內容</h1>"
            }).state('world',{
                url: "/world/:receivedId",
                template: "<h1>這是world跳轉以後對應的內容</h1>",
                controller: 'worldCtrl'
            });
    }]);
    app.controller('worldCtrl', function($stateParams) {
        alert($stateParams.receivedId); 
    });
</script>
</body>
</html>
相關文章
相關標籤/搜索