关于angularjs 使用路由动态加载页面并加载JS的问题

使用angularJs 的 ngRoute 构建单页面应用时因为页面应用比较多想实现动态加载的功能,根据事件尽心路由配置

如果直接使用 绑定模块的配置方法可以获取对应页面数据,

// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', ['ngRoute']);

// configure our routes
scotchApp.config(function($routeProvider) {
    $routeProvider.when('/', {
        controller : 'myCtrl',
        templateUrl : getUrl("wxappList"),
    });
});

scotchApp.controller('mainController', function($scope) {

});

如果在控制器中声名方法就不会执行

var scotchApp = angular.module('scotchApp', ['ngRoute']);

// configure our routes


scotchApp.controller('mainController', function($scope) {
    $scope.run = function(){
        scotchApp.config(function($routeProvider) {
            $routeProvider.when('/', {
                controller : 'myCtrl',
                templateUrl : getUrl("wxappList"),
            });
        });
    }

    $scope.run();
});

因为加载的页面会越来越多所以想实现动态加载的功能,同时加载对应的JS文件。。。

阅读 3k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题