I'm trying to use AngularJS and RequireJS in combination. I would like to use the $routeProvider
service and avoid having to load all the controllers for my views on application startup. For that, I tried the following:
我试着把AngularJS和RequireJS结合使用。我希望使用$routeProvider服务,并且避免在应用程序启动时加载所有的控制器。为此,我尝试了以下方法:
define(['require', 'angular', 'appModule'], function (require, angular, app) {
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/sites', {templateUrl: '/Site/GetSitesView', controller: function() {
require(['sitesController'], function(SitesController) {
return new SitesController();
})
}})
}]);
});
Unfortunately, that did not work for me. There are no errors. The JS file containing the controller is being loaded properly but I cannot see the data bound value in the rendered view. I was wondering whether I could assign the value of controller
in some other way that would wait for the asynchronous call (to load the JS file) to complete.
不幸的是,这对我不起作用。没有错误。包含控制器的JS文件正在被正确加载,但是我在呈现的视图中看不到数据绑定值。我想知道是否可以以其他方式分配控制器的值,以等待异步调用(加载JS文件)完成。
Any ideas?
什么好主意吗?
1 个解决方案
#1
4
You can find the solution here
你可以在这里找到解。
You need to define a resolve property on each route and assign it a function that returns a promise. The function can dynamically load the script contains the target controller and resolve the promise once the loading is complete. In this article, Dan Wahlin shows how to use convention over configuration to have more practical routing.
您需要在每个路由上定义一个解析属性,并为它分配一个返回承诺的函数。该函数可以动态加载包含目标控制器的脚本,并在加载完成后解析承诺。在本文中,Dan Wahlin展示了如何使用约定而不是配置来实现更实际的路由。
#1
4
You can find the solution here
你可以在这里找到解。
You need to define a resolve property on each route and assign it a function that returns a promise. The function can dynamically load the script contains the target controller and resolve the promise once the loading is complete. In this article, Dan Wahlin shows how to use convention over configuration to have more practical routing.
您需要在每个路由上定义一个解析属性,并为它分配一个返回承诺的函数。该函数可以动态加载包含目标控制器的脚本,并在加载完成后解析承诺。在本文中,Dan Wahlin展示了如何使用约定而不是配置来实现更实际的路由。