Angular JS on Rails - Argument 'RaffleCtrl' is not a function, got undefined

前端 未结 1 1834
天涯浪人
天涯浪人 2021-01-25 08:55

I\'m following a tutorial for integer Angular JS on a RoR project.

In my controller js files i\'ve got this:

raffle.coffee

# Pla         


        
相关标签:
1条回答
  • 2021-01-25 09:51

    You must define the ng-app directive in application.html.erb:

    <html ng-app="Raffler">
    ...
    </html>
    

    Then in raffle.js.coffee replace this:

    @RaffleCtrl = ($scope) ->
    

    With this:

    angular.module('Raffler', []).controller "RaffleCtrl", ($scope) ->
    

    You must set the required ng-app directive in your Angular apps--this specifies the element as the root element. Do this in your application layout, on a high-level/root element like a body or html tag. Important note, you can designate this directive once and only once--if you mistakenly add multiple then the first designation in the app will be used to define the root element.

    After you add ng-app, then specify a matching root module using the name you specified in ng-app. Leaving the array empty in the definition indicates that you're defining the module rather than invoking it. It doesn't matter what name you use, just make sure it's something relatively descriptive of your app, unique among your modules and that you specify precisely the same name for your root module that you added in `ng-app. This module will either contain your application code directly, or will have dependencies on other modules that contain it.

    Basically, you must give your application an ng-app attribute, designating the doc's root element. And then you must create your Angular root module using that same name--"Raffler" in this case.

    This should be relevant regardless but FWIW I was using Rails '4.1.5' and the angularjs-rails gem 1.4.4.

    0 讨论(0)
提交回复
热议问题