iT邦幫忙

0

AngularJS error [$injector:modulerr]


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是沒問題的, 是哪裡忽略了嗎?

不明
【**此則訊息已被站方移除**】

1 個回答

2
listennn08
iT邦高手 8 級 ‧ 2021-01-12 10:57:43
最佳解答

對 angularjs 不熟,但你使用 module 去建立 controller 好像不太對,
一般寫法應該是

const app = angular.module('rootApp', []);

app
    .controller('ControllerA', function($scope) { /* ... */ })
    .controller('ControllerB', function($scope) { /* ... */ })
    .directive('dynamic', function ($compile) { /* ... */ })
阿薩姆 iT邦新手 5 級 ‧ 2021-01-12 11:18:06 檢舉

謝謝回覆, 剛剛重新clone code後看起來沒問題了...
我的寫法應該是OK, 但也學到另一種寫法了 Thanks~

對,你的寫法沒有問題,是我搞錯了/images/emoticon/emoticon06.gif

我要發表回答

立即登入回答