美文网首页
An_路由Demo

An_路由Demo

作者: android_en | 来源:发表于2017-09-16 09:57 被阅读19次
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script type="text/javascript" src="js/angular.min.js" ></script>
        <script type="text/javascript" src="js/angular-route.js" ></script>
        
    </head>
    <body ng-app="myApp" >
        <div id="navigator" style="width: 100%;display: inline-block;background-color:red;height:400px>
        <!--菜单-->
        <ul style="float: left;">
            <li style="float: left;"><a href="#/home">首页</a></li>
            <li style="float: left;"><a href="#/woman">女装</a></li>
            <li style="float: left;"><a href="#/man">男装</a></li>
            <li style="float: left;"><a href="#/life">生活用品</a></li>
            <li style="float: left;"><a href="#/cook">厨房用品</a></li>
        </ul>
    </div>
 <div style="width: 80%;display: inline-block;background-color: green;height: 400px">
        <div ng-view=""></div>

    </div>
    </body>
<script type="text/ng-template" id="index.home.html">
    <div style="background: blueviolet;">sss</div>
<div style="background: blueviolet;">sss</div>
</script>   
<script type="text/ng-template" id="index.woman.html">
    <h1>这是女装</h1>
</script>
<script type="text/ng-template" id="index.man.html">
    <h1>这是男装</h1>
</script>
<script type="text/ng-template" id="index.life.html">
    <h1>这是生活馆</h1>
</script>
<script type="text/ng-template" id="index.cook.html">
    <h1>这是厨房用品</h1>
</script>
<script>
     angular.module('myApp',['ngRoute'])
    .controller('HomeController',function ($scope,$route) {
        $scope.$route = $route;
    })
        .controller('WomanController',function ($scope,$route) {
            $scope.$route = $route;
        })
        .controller('WomanController',function ($scope,$route) {
            $scope.$route = $route;
        })
        .controller('ManController',function ($scope,$route) {
            $scope.$route = $route;
        })
        .controller('LifeController',function ($scope,$route) {
            $scope.$route = $route;
        })
        .controller('CookController',function ($scope,$route) {
            $scope.$route = $route;
        })
 
        //配置$routeProvider用来定义路由规则
        //$routeProvider为我们提供了when(path,object)& other(object)函数按顺序定义所有路由,函数包含两个参数:
        //@param1:url或者url正则规则
        //@param2:路由配置对象
        .config(function($routeProvider){
            $routeProvider.
            when('/home',{
                //templateURL:插入ng-view的HTML模板文件
                templateUrl:'index.home.html',
                controller:'HomeController'
 
            }).
            when('/woman',{
            templateUrl:'index.woman.html',
            controller:'WomanController'
            }).
            when('/man',{
                templateUrl:'index.man.html',
                controller:'ManController'
            }).
            when('/life',{
                templateUrl:'index.life.html',
                controller:'LifeController'
            }).
            when('/cook',{
                templateUrl:'index.cook.html',
                controller:'CookController'
            })
        })
</script>
</html>

Demo2

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/angular.js" ></script>
        <script type="text/javascript" src="js/angular-route.js" ></script>
        <script>
            var app=angular.module("myApp",["ngRoute"]);
            app.config(["$routeProvider",function($routeProvider){
                $routeProvider.when("/",{template:"这是主页面"})
                .when("/book",
                {templateUrl:"book.html",
                 controller:"bookcontroller"       
                })
                .when("/car",
                {templateUrl:"car.html",
                 controller:"carcontroller"
                })
                .otherwise({redirectTo:"/"});
            }]);
            app.controller("bookcontroller",function($scope){
                $scope.name="这是图书d";
            });
            app.controller("carcontroller",function($scope){
                $scope.name="这是汽车d";
            });
            app.controller("myCtrl",function($scope,$location){
                $scope.goToUrl=function(path){
                    $location.path(path);
                }
                
            });
        </script>
    </head>
    <body ng-app="myApp" ng-controller="myCtrl">
        <ul>
            <li><a href="#/">主页</a></li>
            <li><a href="#/book">图书</a></li>
            <!--<li><a href="#/baidu">百度</a></li>-->
            <li><a href="#/car">购物车</a></li>
            <li><a href="#/aasdfsdfa">其他</a></li>
            <button ng-click="goToUrl('/')">主页面</button>
        </ul>
        <div ng-view></div>
        <script type="text/ng-template" id="book.html">
            {{name}}
        </script>
        <script type="text/ng-template" id="car.html">
            {{name}}
        </script>
    </body>
</html>

相关文章

网友评论

      本文标题:An_路由Demo

      本文链接:https://www.haomeiwen.com/subject/pijysxtx.html