angular 路由,ui-router,ocLazyLoad的使用

初学angular,有不对的地方请指出来,谢谢

1.angular的路由,模块化,更换html内容来更换页面的展示。

2.路由建立好后,若要同时更换页面的两处(或者多处)不同地方的内容,就要用到ui-router。

3.如果不是小型项目,路由页面所有用到的js、css等全都在首页第一次加载的时候加载进来,体验很差,使用ocLazyLoad来实现按需加载(所加载的js都保存在缓存中,只加载一次,不会重复加载)

下面就来一步一步实现:

一:angular路由(网上教程一大堆,只贴代码)

html

<script src="style/js/angular.min.js"></script>  
<script src="style/js/angular-route.js"></script> 


<body ng-app="myapp">  
    <div class="nav">  
        <ul>  
            <li><a href="#/indexcont">首页</a></li>  
            <li><a href="#/p1">文章</a></li>  
            <li><a href="#/p2">弹窗</a></li>  
            <li><a href="#/p3">输入框</a></li>  
        </ul>  
    </div>  
    <div class="cont" ng-view></div>  
</body>
js

var app = angular.module('myapp',['ngRoute'])  
    .config(['$routeProvider',function($routeProvider) {  
        $routeProvider  
        .when('/indexcont',{templateUrl:'html/indexcont.html'})  
        .when('/p1',{templateUrl:'html/p1.html'})  
        .when('/p2',{templateUrl:'html/p2.html'})  
        .when('/p3',{templateUrl:'html/p3.html'})  
        .otherwise({redirectTo:'/indexcont'});  
    }])


二:同时更换多处内容:ui-router
用ui-router替换ng-route,模块依赖也不同,下面是代码

html

<script src="style/js/angular.min.js"></script>  
<script src="style/js/angular-ui-router.min.js"></script>


<body ng-app="myapp">  
  <div class="nav">  
  <ul>  
    <li><a href="#/indexcont">首页</a></li>  
    <li><a href="#/p1">文章</a></li>  
    <li><a href="#/p2">弹窗</a></li>  
    <li><a href="#/p3">输入框</a></li>  
  </ul>  
  </div>  
    <div class="cont" ui-view="cont1"></div>  
    <div class="cont" ui-view='cont2'></div>  
</body>  


这里,要更换的地方是: ui-view = " "  

js

var app = angular.module('myapp',['ui.router']);  
app.config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider) {  
    $urlRouterProvider.otherwise('/indexcont');  
    $stateProvider  
    .state('indexcont',{  
        url:'/indexcont',  
        views:{  
            "cont1":{templateUrl:'html/indexcont.html'}  
        }  
    })  
    .state('p1',{  
        url:'/p1',  
        views:{  
            "cont1":{templateUrl:'html/p1.html'},  
            'cont2':{templateUrl:'html/p3.html'}  
        }  
    })  
    .state('p2',{  
        url:'/p2',  
        views:{  
            "cont1":{templateUrl:'html/p2.html'}  
        }  
    })  
    .state('p3',{  
        url:'/p3',  
        views:{  
            "cont1":{templateUrl:'html/p3.html'}  
        }  
    })  
}])


写法有多处不同,不过这样功能更强大了。


三:按需加载控制器:ocLazyLoad

    与二相比,只是多了一个依赖模块,在views的后面多了resolve

    引入框架多了一个,html代码不变


html


<script src="style/js/angular.min.js"></script>  
<script src="style/js/angular-ui-router.min.js"></script>  
<script src="style/js/ocLazyLoad.min.js"></script>

<body ng-app="myapp">  
    <div class="nav">  
        <ul>  
            <li><a href="#/indexcont">首页</a></li>  
            <li><a href="#/p1">文章</a></li>  
            <li><a href="#/p2">弹窗</a></li>  
            <li><a href="#/p3">输入框</a></li>  
        </ul>  
    </div>  
    <div class="cont" ui-view="cont1"></div>  
    <div class="cont" ui-view='cont2'></div>  
</body>


js

var app = angular.module('myapp',['ui.router','oc.lazyLoad']);  
app.config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider) {  
    $urlRouterProvider.otherwise('/indexcont');  
    $stateProvider  
    .state('indexcont',{  
        url:'/indexcont',  
        views:{  
            "cont1":{templateUrl:'html/indexcont.html'}  
        }  
    })  
    .state('p1',{  
        url:'/p1',  
        views:{  
            "cont1":{templateUrl:'html/p1.html'},  
            'cont2':{templateUrl:'html/p3.html'}  
        },  
        resolve:{  
            loadMyCtrl:['$ocLazyLoad',function($ocLazyLoad){  
                return $ocLazyLoad.load('style/js/p3.js');  
            }]  
        }  
    })  
    .state('p2',{  
        url:'/p2',  
        views:{  
            "cont1":{templateUrl:'html/p2.html'}  
        },  
        resolve:{  
            loadMyCtrl:['$ocLazyLoad',function($ocLazyLoad){  
                return $ocLazyLoad.load('style/js/p2.js');  
            }]  
        }  
    })  
    .state('p3',{  
        url:'/p3',  
        views:{  
            "cont1":{templateUrl:'html/p3.html'}  
        },  
        resolve:{  
            loadMyCtrl:['$ocLazyLoad',function($ocLazyLoad){  
                return $ocLazyLoad.load('style/js/p3.js');  
            }]  
        }  
    })  
}])



resolve中是控制器地址,仅仅是控制器controller的地址,不是我们写的原生js(或者jquery写的js等)代码脚本,因为resolve里的代码比views中的先加载,不是控制器脚本即使加载进去也不起作用。我们一般的js脚本可以在我们要加载的html代码中引入(这样会重复加载)。

当然,resolve中也可加载css等,可以同时加载多个控制器脚本与css,写成数组,放在括号内即可





Angular Router by Victor Savkin English | 20 Mar. 2017 | ASIN: B06X9N272Y | 118 Pages | AZW3 | 2.44 MB Key Features Written by the creator of the Angular router, giving you the best information straight from the source Get full coverage of the entire Angular Router library and understand exactly how every command works Essential for all serious users of Angular who need to manage states within their applications Book Description Managing state transitions is one of the hardest parts of building applications. This is especially true on the web, where you also need to ensure that the state is reflected in the URL. In addition, you might want to split applications into multiple bundles and load them on demand. Doing this transparently isn't easy. The Angular router solves these problems. Using the router, you can declaratively specify application states, manage state transitions while taking care of the URL, and load bundles on demand. This book is a complete description of the Angular router written by its designer. It goes far beyond a how-to-get-started guide and talks about the library in depth. The mental model, design constraints, and the subtleties of the API-everything is covered. You'll learn in detail how to use the router in your own applications. Predominantly, you'll understand the inner workings of the router and how you can configure it to work with any edge cases you come across in your sites. Throughout the book, you'll see examples from real-world use in the MailApp application. You can view the full source of this application and see how the router code works to manage the state of the application and define what is visible on screen. Reading this book will give you deep insights into why the router works the way it does and will make you an Angular router expert. What you will learn Understand the role of the Angular router and how to make the most of it Build and parse complex URLs Learn about the componentless and empty-path routes Take con
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值