2017-02-15 65 views
4

我正在mvc-angularjs中创建一个web应用程序,我正在使用ngroute。这个名字的控制器没有在angularjs ngroute中注册

<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> 

,这是我的欢迎页面:在我的布局与HTML代码

Angularjs控制器

<div ng-controller="welcome"> 

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

    }); 
</script> 

,当我跑我得到了以下错误代码

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

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

当我将我的应用程序从('MyApp',[])更改为('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.

回答

5

angular.module可以创建一个新的模块或检索现有的模块。这取决于你使用的语法。

这也是referenced上的官方角度文档

例如

angular.module('MyApp',[])创建一个新模块,因为它包含依赖注入数组(作为第二个参数)。

angular.module('MyApp')是现有模块的参考,因为它不包含依赖注入数组。

在你的情况下,它会在第一个代码块上创建一个新模块。因此,在第二个代码块中,您需要检索它。

可能,如果你在代码的第二块改变

angular.module('MyApp',[])angular.module('MyApp')问题将得到解决。

+0

模块'MyApp'不可用!您拼错了模块名称或忘记加载模块名称。如果注册模块确保您指定依赖关系作为第二个参数。 这是我得到的错误 –

+0

这是你从我的答案更新代码后得到的错误? – Korte

+0

是的,这是我得到的 –

相关问题