2015-01-05 112 views
4

我正在使用angularjs和cassandra在節點應用程序上工作。 當我去頁面如註冊頁面,然後前端正常到來,但它顯示錯誤,控制器是未定義的。 錯誤:錯誤:AREQ 爲參數Angularjs控制器顯示未定義?

這是app.js(配置文件主要JS)

angular.module('app', ['ui.router']) 
.config(['$urlRouterProvider', '$stateProvider', 
    function($urlRouterProvider, $stateProvider) { 
     $urlRouterProvider.otherwise('/'); 
     $stateProvider 
      .state('home', { 
       url: '/', 
       templateUrl: '/browser/views/home.html' 
      }) 
      .state('register', { 
       url: '/register', 
       templateUrl: '/browser/views/register.html', 
       controller: 'RegisterCtrl' 
      }) 
      .state('login', { 
       url: '/login', 
       templateUrl: '/browser/views/login.html', 
       controller: 'LoginCtrl' 
      }) 
    } 
]); 

這是register.html文件隨後由控制器的路徑。

<div ng-controller="RegisterCtrl" class="container-fluid"> 
<div class="row custom-row2"> 
    <div class="col-md-6 col-lg-6 col-sm-6"> 
     <h4 class="sign_up">{{ title }}</h4> 
     <form class="form-horizontal" id="login_form" name="login_form" role="form"> 
      <div class="form-group"> 
       <label class="control-label col-sm-3" for="name">Name:</label> 
       <div class="col-sm-9"> 
        <input ng-model="name" type="text" class="form-control" name="field1" id="name" aria-describedby="inputSuccess3Status" placeholder="Enter Your Name...."> 
       </div> 
      </div> 
     </form> 
    </div> 
</div> 

這是它在Firebug的檢查

angular.module('app', ['ui.router']) 
    .controller('RegisterCtrl', ['$scope', 
     function($scope, RegiserService) { 
      $scope.title = "Sign Up"; 
     } 
    ]); 

冠軍其中有即將來自registerCtrl控制器registerCtrl.js文件不是來和{{title}}即將在那個地方。

我已經經歷了幾次這樣的事情,但沒有能夠得到定義控制器時出了什麼問題。

+1

你做'angular.module( '應用',[ 'ui.router'])。controller'再重新創建應用程序。在創建應用程序後,只需使用getter語法'angular.module('app')'來獲取模塊。 – PSL

+0

我沒有找到你?請解釋 ?? – ashishkumar148

+0

我已經添加了解釋與文檔鏈接的答案..這是否解決您的問題? – PSL

回答

3

您正在重新創建模塊angular.module('app', ['ui.router']).controller.創建應用程序後,只需使用getter語法angular.module('app')即可獲取模塊。我假設你在註冊服務RegiserService時也是這樣做的,這些服務清理了其他註冊到同一模塊app的其他內容。通過使用語法angular.module('moduleName', [..dep])創建模塊後,您應該在註冊任何其他後續實體的同時獲取具有getter語法angular.module('app')的模塊。

模塊創建(只有一次):

angular.module(moduleName, [requires], [configFn]); 

模塊吸氣劑(訪問已經創建的模塊):

angular.module(moduleName); 

documentation

When passed two or more arguments, a new module is created. If passed only one argument, an existing module (the name passed as the first argument to module) is retrieved.

因此,例如: -

angular.module('app').controller('RegisterCtrl',... 
angular.module('app').service('RegiserService',... 
+0

你也可以這樣做,並使用'app',但是你會污染全局命名空間。相反,你可以使用getter語法,因爲我已經提到'angular.module('app').controller ...;'。你可以用'angular.module('app',['ui.router'])''來創建模塊一次。這說明了嗎? – PSL

+0

我曾試過,但控制器仍未定義。 – ashishkumar148

+0

@ashish__k它非常簡單和基本。你必須在其他地方重新定義模塊。我們不僅可以根據您向我們展示的內容提供幫助。你也需要'ng-app =「app」'在你的html中。除了我們向我們展示的內容之外,還有很多其他的東西可能會與我們分開。 – PSL

1

除了PSL提到的重新定義模塊, 還有一點是 - 什麼是RegiserService?如果是某種服務,那麼控制器的定義是錯誤的。

正確的定義是

angular.module('app', ['ui.router']) 
    .controller('RegisterCtrl', ['$scope', 'RegiserService', 
     function($scope, RegiserService) { 
      $scope.title = "Sign Up"; 
     } 
    ]); 
+0

這是用於未在上面給出的部分中使用的進一步使用。 – ashishkumar148

+0

但是它應該已經作爲服務可用,並在'scope'之後附加,否則應該將其從控制器定義中刪除。 –

+0

是啊,它是目前沒有顯示,因爲我只寫了部分代碼給出問題的性質的想法。 – ashishkumar148