4

I am creating a web app in mvc-angularjs, I am using ngroute.

Angularjs controller in my layout with html code:

<div class="container body-content" ng-app="MyApp">
    <br />
    <br />
    <br />
    <div style="float:right;">
        <a href="#" class="btn btn-danger">Sign Out</a>
    </div>

    @RenderBody()
    <hr />
</div>

<script>
    (function () {

        var app = angular.module('MyApp', ['ngRoute', "ngStorage"]);  

        app.controller('myctrllayout', function ($scope) {  

        });
    })();
 </script>

and this is my welcome page:

<div ng-controller="welcome">

</div>
<script>
    angular.module('MyApp',[]) 
    .controller('welcome', function ($scope) { 

    });
</script>

when I run the code I got the following error

Error: $controller:ctrlreg A controller with this name is not registered.

The controller with the name 'welcome' is not registered.

when I change my app from ('MyApp',[]) to ('MyApp'):

Module 'MyApp' 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.

1 Answer 1

5

angular.module Can create a new module or retrieve an existing. This depends on the syntax you use.

This is also referenced on the official angular documentation.

e.g.

angular.module('MyApp',[]) creates a new module because it includes the dependency injection array (as second parameter).

angular.module('MyApp') is a reference on the existing module because it does not include the dependency injection array.

In your case it creates a new module on the first block of code. So in the second block of code you need to retrieve it.

Possibly the problem would be solved if you change

angular.module('MyApp',[]) to angular.module('MyApp') in the second block of code.

Sign up to request clarification or add additional context in comments.

4 Comments

Module 'MyApp' 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. This was the error i was getting
Is this the error you get after you updated code from my answer ?
Also remove from the first block the declaration of var app so it will be: angular.module('MyApp', ['ngRoute', "ngStorage"]) .controller('myctrllayout', function ($scope) { });
i declared that on my layout page,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.