【发布时间】:2017-04-08 11:01:56
【问题描述】:
在 AngularJS 1.2 中,我有一些代码可以动态加载路由和控制器,效果很好。我想升级到 AngularJS 1.6,现在我的动态不再起作用了。
这是我加载 JS 文件并重新计算路线的旧代码(有点简化,因为我有一些有效性验证,但这是基础):
moduleApp.config(function ($routeProvider, $controllerProvider) {
moduleApp.controllerProvider = $controllerProvider.register;
moduleApp.routeProvider = $routeProvider;
$routeProvider.when('/', {
templateUrl: 'startpage.html',
controller: 'startpageController'
})
.otherwise({
resolve: {
deps: function($q, $rootScope, $location) {
var deferred = $q.defer();
var modulename = $location.path().split("/")[1];
if (modulename !== null) {
// Load the JS using https://github.com/ded/script.js (Old but it works :)
$script(modulename + ".js", function() {
$rootScope.$apply(function() {
$rootScope.$broadcast('$locationChangeSuccess', $location.path(), $location.path());
});
});
}
return deferred.promise;
}
}
});
});
正在加载的 JS 文件如下所示:
moduleApp.routeProvider.
when('/FirstModule', {
templateUrl: 'FirstModule.html',
caseInsensitiveMatch: true
});
moduleApp.controllerProvider('firstModuleController', function ($scope) {
});
同样,这在 1.2 中可以正常工作,但我相信在 1.6 中应用该路线不再以这种方式工作。那么我需要在使用$rootScope 的函数内部进行什么更改才能使其再次工作?还是我还需要改变更多?万一呢?
我用这个代码创建了一个Plunker。
在index.html 中将 AngularJS 版本更改为 1.2.16,以查看它在 1.2 中是否有效(请记住还将链接中的 hashbang 更改为仅 # 而不是 #!)。改回1.6就不行了。
【问题讨论】:
-
请定义“不起作用”
-
我点击Plunker中的“第一个模块”链接,路由的
otherwise部分被调用,FirstModule.js被加载。但是,模块的内容从不显示。
标签: javascript angularjs ngroute