Angular JS on Rails - 参数'RaffleCtrl'不是一个函数,没有定义 [英] Angular JS on Rails - Argument 'RaffleCtrl' is not a function, got undefined

查看:100
本文介绍了Angular JS on Rails - 参数'RaffleCtrl'不是一个函数,没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的控制器js文件中,我得到了这个:



raffle.coffee

 #将所有行为和钩子与这里的匹配控制器有关。 
#所有这些逻辑将自动在application.js中可用。
#您可以在此文件中使用CoffeeScript:http://coffeescript.org/

@RaffleCtrl =($ scope) - >
$ scope.entries = [

{name:name1}
{name:name2}
{name:name3}
{name:name4}
]

Rails)我得到了这个:

index.html.erb

 < H1>抽奖< / H1> 

< div ng-controller =RaffleCtrl>
< form>
< input type =textng-model =newEntry.name>
< / form>

< ul>
< li ng-repeat =条目中的条目>
{{entry.name}}
< / li>
< / ul>
< / div>

所以它应该重复数组条目中的四个名称,但它不起作用:我打开日志和我发现这个错误:
$ b


参数'RaffleCtrl'不是函数,没有定义

blockquote>

解决方案

您必须定义 ng-app 指令 application.html.erb

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

然后在 raffle.js.coffee 替换这:

  @RaffleCtrl =($ scope) - > 

有了这个:

  angular.module('Raffler',[])。controllerRaffleCtrl,($ scope) - > 

您必须设置所需的 ng-app 指令在您的Angular应用程序中 - 它将元素指定为根元素。在应用程序布局中,在高级/根元素(如 或 html 标记)上执行此操作。重要说明,您可以一次性指定一次 - 如果您错误地添加了多个,那么应用中的第一个指定将用于定义根元素。



添加 ng-app 后,使用您在<$中指定的名称指定匹配的根模块 C $ C> NG-应用。在定义中保留数组为空意味着您正在定义模块而不是调用它。不管你使用什么名字,只要确保它是相对描述你的应用的东西,在你的模块中是唯一的,并且你指定了与你在`ng-app中添加的根模块完全相同的名字。该模块将直接包含您的应用程序代码,或者将依赖于包含它的其他模块。

基本上,您必须为您的应用程序指定一个 ng-app 属性,指定文档的根元素。然后,您必须使用相同的名称创建您的Angular根模块 - 在这种情况下为Raffler。



无论FWIW我使用的是什么,这应该是相关的 Rails'4.1.5' angularjs-rails gem 1.4.4 。


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

In my controller js files i've got this:

raffle.coffee

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

@RaffleCtrl = ($scope) ->
    $scope.entries = [  

        {name:"name1"}
        {name:"name2"}
        {name:"name3"}
        {name:"name4"}
    ] 

and in my controller html (generated by rails) i've got this:

index.html.erb

<h1>Raffle</h1>

<div ng-controller="RaffleCtrl">
    <form>
        <input type="text" ng-model="newEntry.name">
    </form>

    <ul>
        <li ng-repeat="entry in entries">
            {{entry.name}}
        </li>
    </ul>
</div>

so it should repeat the four name in the array entries but it doesn't work: i open the log and i found this error:

Argument 'RaffleCtrl' is not a function, got undefined

解决方案

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.

这篇关于Angular JS on Rails - 参数'RaffleCtrl'不是一个函数,没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆