我正在使用 ng-view 来尝试重用常见页面元素。也许我误解了这个观点,在这里不合适。
基本上,我有一个如下所示的 html 页面:
<html>
<head>
<!-- css and js here including index.js -->
</head>
<body ng-app="app" ng-controller="ctrl">
<navigation-bar/>
<div ng-view />
</body>
</html>
这是我的 index.js 的样子
(function () {
angular.module('app', ['ngRoute', 'ngSanitize'], function ($routeProvider) {});
angular.module('app').controller("ctrl", function ($scope, $log, $q, $timeout) {});
angular.module('app').config(["$routeProvider", function ($routeProvider) {
$routeProvider.when("/management", {
templateUrl : "/experiment/management.html",
// Is there a way to load /experiment/managementController.js here?
}
})
]);
})();
我的管理.html
<div ng-controller="managementController">
<!-- A bunch of code -->
</div>
还有managementController.js(我想仅在加载路由时运行)
(function () {
angular.module('app').controller("managementController", [
"$scope", "$sce", "$log", function ($scope, $sce, $log) {
// controller specific code
}
]);
})();
我为什么要这样做?因为我对很多潜在的控制者有很多很多潜在的观点。 (50个左右)
这是不可能的还是不是 ng-view 的意图?
如果这不是 ng-view 的意图,在这种情况下有什么可以帮助我吗?
最佳答案
Take a look at the first example在文档中。查看 script.js 选项卡。
$routeProvider.when("/management", {
templateUrl : "/experiment/management.html",
controller: 'managementController',
}
如果您的意思是要加载实际文件,take a look at this post
关于javascript - 使用 ng-view 并按需加载 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31460775/