AngularJS 动态加载控制器实例-ocLoazLazy
一、AngularJS动态加载控制器和视图实例
路由配置关键代码:
[javascript] view plain copy print?
- //二级页面
- $stateProvider.state('main', {
- url: '/:name',
- /**如果需要动态加载数据和控制器***/
- views: {
- "": {
- templateUrl: function ($stateParams) {
- console.info($stateParams);
- return '/template/' + $stateParams.name + '.html';
- },
- resolve: {
- des: function ($ocLazyLoad, $stateParams) {
- if ($stateParams.name == 'about')
- return;
- return $ocLazyLoad.load('/scripts/myjs/' + $stateParams.name + '.js');
- }
- }
- }
- }
- });
1.引用:
[html] view plain copy print?
- <script src="../Scripts/jquery-1.10.2.min.js">script>
- <script src="../Scripts/Angular/angular.min.js">script>
- <script src="../Scripts/Angular/ocLazyLoad.min.js">script>
- <script src="../Scripts/Angular/angular-ui-router.min.js">script>
- <link href="../Content/bootstrap.min.css" rel="stylesheet" />
2.HTML
[html] view plain copy print?
- <div class="container" ng-app="myApp">
- <ul class="nav nav-pills">
- <li role="presentation"><a href="#/home">首页a>li>
- <li role="presentation"><a href="#/about">关于页面a>li>
- ul>
- <div class="panel panel-danger">
- <div class="panel-heading">
- <div class="panel-title">模板内容div>
- div>
- <div class="panel-body" ui-view>div>
- div>
- div>
3.Js
[javascript] view plain copy print?
-
- var app = angular.module('myApp', ['oc.lazyLoad', 'ui.router']);
- //配置路由
- app.config(function ($stateProvider, $urlRouterProvider) {
- $urlRouterProvider
- .when('', '/');
- //首页
- $stateProvider.state('index', {
- url: '/',
- templateProvider: function ($stateParams) {
- console.info('这是首页');
- return '
这是首页
'; - }
- });
- //二级页面
- $stateProvider.state('main', {
- url: '/:name',
- /***如果只是动态加载视图**/
- //templateUrl: function ($stateParams) {
- // console.info($stateParams);
- // return '/template/' + $stateParams.name + '.html';
- //},
- //controller: 'homeController',
- /**如果需要动态加载数据和控制器***/
- views: {
- "": {
- templateUrl: function ($stateParams) {
- console.info($stateParams);
- return '/template/' + $stateParams.name + '.html';
- },
- resolve: {
- des: function ($ocLazyLoad, $stateParams) {
- if ($stateParams.name == 'about')
- return;
- return $ocLazyLoad.load('/scripts/myjs/' + $stateParams.name + '.js');
- }
- }
- }
- }
- });
- });
- 本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/p/6771532.html,如需转载请自行联系原作者
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!