我有一个使用AngularJS的Web应用程序,但每个视图都有不同的JS文件。如何在路由when
控制器中加载JS文件?
var app = angular.module("myApp", ['ngRoute']);
app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/member/master', {
templateUrl: 'templates/two-grid.html',
controller: 'memberMaster'
}).
when('/core/master', {
templateUrl: 'templates/two-grid.html',
controller: 'coreMaster'
}).
when('/user/master', {
templateUrl: 'templates/two-grid.html',
controller: 'userMaster'
}).
otherwise({
redirectTo: '/dashbourd'
});
}]);
app.controller('coreMaster', function ($scope) {
//I want to load js/coremaster.js here
});
app.controller('memberMaster', function ($scope) {
//I want to load js/membermaster.js here
});
app.controller('userMaster', function ($scope) {
//I want to load js/usermaster.js here
});
答案 0 :(得分:1)
我认为$ stateProvider和ocLazyLoad可以帮助你。
查看示例:
$stateProvider
.state('login',{
templateUrl:'views/pages/login.html',
controller: 'LoginCtrl',
controllerAs: 'vm',
url:'/login',
resolve: {
loadMyDirectives:function($ocLazyLoad){
return $ocLazyLoad.load(
{
name:'aExample',
files:[
'scripts/controllers/login.js',
]
});
}
}
})
您可以在此处查看其他示例:https://github.com/ocombe/ocLazyLoad/blob/master/examples/complexExample/js/app.js