2014-01-17 101 views
0

我看了大約10回答類似的問題,仍然想不通這一個。參數「PostCtrl」不是一個函數,得到了不確定

我嘗試一些角度添加到已有的Rails應用程序我有,並按照these tips啓動。

所以我有這個在我的應用程序:

application.coffee

#= require jquery 
#= require jquery_ujs 
#= require bootstrap 
#= require libraries/angular.min 
#= require libraries/angular-resource.min 
#= require_tree . 
#= require main-ng 

那麼這

main-ng.coffee

#= require_tree ./controllers 

angular.module('Posts', []) 

@MainApp = angular.module("MainApp", [ 
    # modules the app depends on -- this is all I've got so far. 
    "Posts" 
]) 

個佈局/ application.html.haml這是我的所有關於角:

%html{ 'ng-app' => "MainApp" } 

出於某種原因,在做ng_app: 'MainApp'沒有在這裏工作,雖然這句法似乎在其他地方工作(並在其他成立使用Angular編寫的應用程序)。

再就是隻是與控制器的觀點:

帖子/ show.html.haml

.row.container{ ng_controller: 'PostCtrl' } 
# Using hashrocket syntax brings up the same error, while misspelling it does nothing, 
# so this syntax appears to be working here 

最後,控制器:

控制器/ PostCtrl.coffee

#= require directives/markdown 

postModule = angular.module("Posts", ["markdown"]) 
console.log postModule # returns angular object on page load 

ctrl = postModule.controller "PostCtrl", ($scope, $http, $location) -> 

    $s = $scope 

    console.log 'Inside controller' # doesn't get hit 

console.log ctrl # returns angular object on page load 

任何想法我在這裏做什麼noobish錯誤?我在這裏撞牆了。

可能的線索 - 更改視圖中控制器的名稱會更改錯誤中的名稱,但沒有控制檯日誌,因此它基本上似乎是找到該特定控制器的錯誤。

另一個值得注意的事情 - 需要在我main-ng.coffee文件中的JS控制器,目錄和聲明MainApp模塊,而不是帖子上的控制器模塊都似乎沒有任何改變。

回答

3

我看到你後聲明模塊兩次

angular.module('Posts', [])

postModule = angular.module("Posts", ["markdown"])

我建議複製第二個和第一個替換。然後移除第二個模塊。

這兩種語法創建新的模塊,所以加一早先被覆蓋。

+0

賓果。謝謝。這很奇怪,因爲我們在使用Angular的另一個應用程序中肯定使用了雙聲明語法。我必須問問它在那裏的工作原理。無論如何,明白這一點。爲我節省了一些重大的麻煩。 – Sasha

相關問題