【发布时间】:2015-05-01 17:56:56
【问题描述】:
我想在通过视图加载控制器时添加动态控制器的脚本。
这是我的文件树:
- index.html
- app.js
- 浏览量
- prod.html
- cat.html
- prod.html
- 控制器
- prod.js
- cat.js
我希望当用户获得/prod/ URL 时,应用程序将动态加载(在视图中)prod.html 和prod.js 用于控制器的逻辑。
当然,所有这些逻辑都需要 ng-route。
Index.html
<body data-ng-app="myApp">
<div data-ng-view=""></div>
</body>
Default.js 使用 AngularJS v1.3.14
var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider, $locationProvider, $controllerProvider, $provide) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
app.registerCtrl = $controllerProvider.register;
$routeProvider.
when('/:name', {
templateUrl: function (urlAttr) {
return '/views/' + urlAttr.name + '.html';
}
});
// This code throw error:
//TypeError: undefined is not a function
//at angular.min.js:1
//at r (angular.min.js:1)
//at at (angular.min.js:1)
//at b (angular.min.js:1)
//at b (angular.min.js:1)
//at b (angular.min.js:1)
//at b (angular.min.js:1)
//at $get.t (angular.min.js:1)
//at angular.min.js:1
//at p.$get.p.$eval (angular.min.js:1)
$provide.decorator('$controller', ['$delegate', function ($delegate) {
// I want to get the controller name and than load the js file.
// return function (constructor, locals) {
// if (typeof constructor == "string") {
// locals.$scope.loadControllerScript(constructor);
// }
// return $delegate(constructor, locals);
// }
}]);
});
Prod.html
<div class="row" data-ng-controller="prodCtrl">
{{test}}
</div>
Prod.js
app.registerCtrl('prodCtrl', function ($scope) {
$scope.test = '';
});
问题是错误:“undefined is not a function”。 (参见 Default.js 中的代码)
如果问题不清楚,我很乐意解释更多。
【问题讨论】:
-
你能展示你提供 loadControllerScript 函数的代码吗?
-
不相关。 “$provide.decorator('$controller', ['$delegate', function ($delegate) {” - 在“loadControllerScript”之前抛出的异常。我会更新我的问题。
标签: javascript jquery html angularjs