嗨
angularJS v1.7.8
錯誤訊息如下:
Uncaught Error: [$injector:modulerr] Failed to instantiate module rootApp due to:
Error: [$injector:nomod] Module 'rootApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
相關程式碼如下:
<head>
<script src="../javascripts/angular.js"></script>
</head>
<main role="main" " ng-app="rootApp" id="Main">
...
<form ng-controller="ControllerA" id="ID1">
...
</form>
<form ng-controller="ControllerB" id="ID2">
...
</form>
</main>
var rootApp = angular.module('rootApp', ['ID1','ID2']);
var AddApp = angular.module('ID1', []);
var SearchApp = angular.module('ID2', []);
AddApp.controller('ControllerA', function($scope, $http) {
...
});
SearchApp.controller('ControllerB', function($scope, $http, $sce, $q) {
...
});
SearchApp.directive('dynamic', function ($compile) {
return {
restrict: 'EA',
replace: true,
link: function (scope, ele, attrs) {
scope.$watch(attrs.dynamic, function(html) {
ele.html(html);
$compile(ele.contents())(scope);
});
}
};
});
我另一支js單使用一個controller是沒問題的, 是哪裡忽略了嗎?
對 angularjs 不熟,但你使用 module 去建立 controller 好像不太對,
一般寫法應該是
const app = angular.module('rootApp', []);
app
.controller('ControllerA', function($scope) { /* ... */ })
.controller('ControllerB', function($scope) { /* ... */ })
.directive('dynamic', function ($compile) { /* ... */ })