ruby-on-rails - rails + Angularjs : Module 'ngResource' is not available

标签 ruby-on-rails ruby angularjs

我正在尝试将一些 Angularjs 代码合并到测试应用程序中,但无法使其工作。我尝试与数据库进行调用/交互。

这是我做的:

安装了 gem 'angularjs-rails' gem。

application.html.erb中:

<html ng-app="MongoAngularApp">
  ...
  <div class="container" ng-controller="PostsCtrl">
    <%= yield %>
  </div>
  ...

链接:

<%= link_to 'Delete', '', :'ng-click' => "destroy(#{post.id})", :'data-confirm' => 'Are you sure?', :id => "post_#{post.id}" %>

Angularjs 代码在 posts.js 文件中:

var app = angular.module('MongoAngularApp', ['ngResource']);

app.factory('Post', function($resource) {
    return $resource("posts/:id", { id: '@id' }, {
        index:   { method: 'GET', isArray: true, responseType: 'json' },
      show:    { method: 'GET', responseType: 'json' },
      update:  { method: 'PUT', responseType: 'json' }
    });
})

app.controller("PostsCtrl", function($scope, Post) {
  $scope.posts = Post.index();

  $scope.deletePost = function(index) {  
        alert("!");
    post = $scope.posts[index];
    Post.delete(post);
    $scope.posts.splice(index, 1);
  }
})

当我运行它时,在 JS 控制台中我看到以下错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module MongoAngularApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngResource due to:
Error: [$injector:nomod] Module 'ngResource' 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.

我仔细检查了模块名称,这很好。我尝试了一个与 Angularjs 的简单绑定(bind)并且它工作正常,所以 Angularjs 也应该被正确加载。

ngResource 有什么问题?网上查了一些教程,都是用的一样angular.module('angular_app_name', ['ngResource'])

我在这里错过了什么?

谢谢。

编辑:

我正在使用 AngularJS v1.4.7

最佳答案

确保在 application.js 中有这两行

//= require angular
//= require angular-resource

关于ruby-on-rails - rails + Angularjs : Module 'ngResource' is not available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33155039/

相关文章:

ruby-on-rails - 启动服务器绑定(bind)到 0.0.0.0 和 127.0.0.1 有什么区别?

ruby-on-rails - Prawn :锚定文本中间到给定点

javascript - AngularJS - 如何使用 ng-repeat 重复列表中的每 2 个项目?

ruby-on-rails - ActiveSupport::TimeZone 偏移量字符串列表

ruby-on-rails - 使用Sidekiq进行事件作业并获取ActiveJob::DeserializationError

ruby-on-rails - 我在哪里放置 Stripe 的可发布 key 和 key ?

ruby - 检查一个整数是否在一个范围内

ruby - 将函数存储在散列中键的值中

javascript - AngularJS 范围 $id 的值是如何设置的?似乎没有增量。

javascript - 同步两个 Angular ui bootstrap 轮播