


AngularJs solution to dynamically load Controller based on the visited page_AngularJS
The purpose of using Ng is to make a single page application (simple page application). I hope that all pages in the site will use Ng’s Route and try not to use location.href. However, there are many advantages to such a webapp, but the only drawback is that when you As time goes by, webapps have more users, richer functions, and more controllers. You have to load all controllers as global modules so that you can press on any page in the site. After refreshing with F5, you can route to any other page without the error of not finding the controller. Loading all the controllers makes the first opening of the page slower on the mobile phone. Today I will share with you how I improved it. To solve this shortcoming, implement modular loading of Controller
app.js
app.config(function($controllerProvider, $compileProvider, $filterProvider, $provide) {
app.register = {
Controller: $controllerProvider.register,
directive: $compileProvider.directive,
Filter: $filterProvider.register,
Factory: $provide.factory,
Service: $provide.service
};
});
Block during route to load the required js, and then continue after the loading is successful. If you don’t know what $script is, please click http://dustindiaz.com/scriptjs
$routeProvider.when('/:plugin', {
templateUrl: function(rd) {
Return 'plugin/' rd.plugin '/index.html';
},
resolve: {
Load: function($q, $route, $rootScope) {
var deferred = $q.defer();
var dependencies = [
'plugin/' $route.current.params.plugin '/controller.js'
];
$script(dependencies, function () {
$rootScope.$apply(function() {
deferred.resolve();
});
});
Return deferred.promise;
}
}
});
controller.js
app.register.controller('MyPluginCtrl', function ($scope) {
...
});
index.html
...
In this way, the js that the route depends on can be dynamically loaded during the route. However, generally there are many routes in our webapp, and each one requires a lot of code to be written, which is ugly and difficult to maintain. We might as well add another Optimize it
app.js
app.config(function($controllerProvider, $compileProvider, $filterProvider, $provide) {
app.register = {
controller: $controllerProvider.register,
directive: $compileProvider.directive,
filter: $filterProvider.register,
factory: $provide.factory,
service: $provide.service
};
app.asyncjs = function (js) {
return ["$q", "$route", "$rootScope", function ($q, $route, $rootScope) {
var deferred = $q.defer();
var dependencies = js;
if (Array.isArray(dependencies)) {
for (var i = 0; i < dependencies.length; i ) {
dependencies[i] = "?v=" v;
}
} else {
dependencies = "?v=" v;//v是版本号
}
$script(dependencies, function () {
$rootScope.$apply(function () {
deferred.resolve();
});
});
return deferred.promise;
}];
}
});
$routeProvider.when('/:plugin', {
templateUrl: function(rd) {
return 'plugin/' rd.plugin '/index.html';
},
resolve: {
load: app.asyncjs('plugin/controller.js')
}
});
到此只要把原来一个controller.js按模块拆分成多个js然后为route添加模块依赖便可提高加载速度,这个方法不仅仅可以用在controller按需加载,而且可以用在其他js模块,例如jquery.ui.datepicker.js这样的日期选择插件,在需要日期选择插件的route节点加上
$routeProvider.when('/:plugin', {
templateUrl: function(rd) {
return 'plugin/' rd.plugin '/index.html';
},
resolve: {
load: app.asyncjs(['plugin/controller.js','plugin/jquery.ui.datepicker.js'])
}
});
便可以了
PS:$script可以对需要加载的js进行判断,如果之前已经加载过了他会直接返回成功,也就是说只有在第一次进入日期选择界面时会去请求jquery.ui.datepicker.js退出去再进就不会去请求啦

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When a SpringBoot novice creates a project, the Controller cannot be scanned for a series of problems 1.2.3.4.5.6. Another way is to add @ComponentScan(basePackages={"xxx.xxx.xx","xxx.xxx" when starting the service class) .xx”}) is the fully qualified name of the package, which can be used for multiple SpringBoot custom controllers. The SpringBoot custom controller route cannot be scanned and cannot be found because the startup class and the custom Controller package are not in the same directory. Officially recommended placement of application.java

Preface In some cases, the prefixes in the service controller are consistent. For example, the prefix of all URLs is /context-path/api/v1, and a unified prefix needs to be added to some URLs. The conceivable solution is to modify the context-path of the service and add api/v1 to the context-path. Modifying the global prefix can solve the above problem, but there are disadvantages. If the URL has multiple prefixes, for example, some URLs require prefixes. If it is api/v2, it cannot be distinguished. If you do not want to add api/v1 to some static resources in the service, it cannot be distinguished. The following uses custom annotations to uniformly add certain URL prefixes. one,

Javascript is a very unique language. It is unique in terms of the organization of the code, the programming paradigm of the code, and the object-oriented theory. The issue of whether Javascript is an object-oriented language that has been debated for a long time has obviously been There is an answer. However, even though Javascript has been dominant for twenty years, if you want to understand popular frameworks such as jQuery, Angularjs, and even React, just watch the "Black Horse Cloud Classroom JavaScript Advanced Framework Design Video Tutorial".

In today's information age, websites have become an important tool for people to obtain information and communicate. A responsive website can adapt to various devices and provide users with a high-quality experience, which has become a hot spot in modern website development. This article will introduce how to use PHP and AngularJS to build a responsive website to provide a high-quality user experience. Introduction to PHP PHP is an open source server-side programming language ideal for web development. PHP has many advantages, such as easy to learn, cross-platform, rich tool library, development efficiency

With the continuous development of the Internet, Web applications have become an important part of enterprise information construction and a necessary means of modernization work. In order to make web applications easy to develop, maintain and expand, developers need to choose a technical framework and programming language that suits their development needs. PHP and AngularJS are two very popular web development technologies. They are server-side and client-side solutions respectively. Their combined use can greatly improve the development efficiency and user experience of web applications. Advantages of PHPPHP

When using the PHP framework, you often encounter errors such as "PHPFatalerror: Class'Controller'notfound". This kind of error is usually related to the naming, location or loading of files in the framework, especially when you try to use controllers. This article will introduce several common processing methods to solve this problem. Confirm file location First, you need to confirm that the controller file is located in the correct directory for the framework. For example, if you are using the Laravel framework

With the rapid development of Web technology, Single Page Web Application (SinglePage Application, SPA) has become an increasingly popular Web application model. Compared with traditional multi-page web applications, the biggest advantage of SPA is that the user experience is smoother, and the computing pressure on the server is also greatly reduced. In this article, we will introduce how to build a simple SPA using Flask and AngularJS. Flask is a lightweight Py

With the popularity and development of the Internet, front-end development has become more and more important. As front-end developers, we need to understand and master various development tools and technologies. Among them, PHP and AngularJS are two very useful and popular tools. In this article, we will explain how to use these two tools for front-end development. 1. Introduction to PHP PHP is a popular open source server-side scripting language. It is suitable for web development and can run on web servers and various operating systems. The advantages of PHP are simplicity, speed and convenience
