2017-02-15 60 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

是的,這是我得到的 –

相關問題