【问题标题】:Using Restangular with Rails Error: [$injector:modulerr]使用带有 Rails 错误的 Restangular:[$injector:modulerr]
【发布时间】:2014-08-25 04:50:15
【问题描述】:

编辑:Restangular 取决于 Lodash 或 Underscore!安装 Lodash 后,一切正常


怎么样?我只想做一个基本的连接以开始......

这是我的控制器

AngulaRails.controller "BooksController", ($scope, Restangular) ->

allbooks = Restangular.all('books')

$scope.getBooks = ()  ->
  allbooks.getList().then (response) ->
    console.log(response)
    $scope.books = response

这是我在 Rails 中的视图(我并没有在角度混淆路线,只是为了简单起见):

<div ng-app="AngulaRails" ng-controller="BooksController" ng-init= "getBooks()">
  <h3>booksbooksbooks</h3>
  <div>{{ books }}</div>
</div>

你看我正在调用控制器中定义的 getBooks() 函数

这是我的 Rails 控制器:

class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy]


  # GET /books
  def index
    @books = Book.all
    @pages = Page.all

    respond_to do |format|
      format.html {}
      format.json {render json: @books, root: false, each_serializer: BookSerializer }

    end
  end

我知道里面有一些关于 root 和序列化程序等奇怪的东西,但我也试过只渲染 json:@books 无济于事。

我的浏览器会显示 Booksbooksbooks,然后只是字面上的 {{ books }} 这是来自我的控制台的错误消息:

错误:[$injector:modulerr] http://errors.angularjs.org/1.2.8/$injector/modulerr?p0=AngulaRails&p1=%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.8%2F% 24injector%2Fmodulerr%3Fp0%3Drestangular%26p1%3D_%2520is%2520not%2520defined%250Aa.init%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Frestangular.min.js%253Fbody%253D1%253A0%253A7%2 2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Frestangular.min.js%253Fbody%253D1%253A7%253A10268%250Ad%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fassets%252Fassets%252F3angularmin. 253A31%253A451%250Ag%252F%253C.instantiate%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A32%253A585%250Ac%25400http%253A%253D1%253A32%253A585%250Ac%25400http%253%253A%2 252Fassets%252Fangular.min.js%253Fbody%253D1%253A29%253A499%250Aa%252F%253C%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%28%250A2%253D1%28%250A2e% 252F%253C%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fang ular.min.js%253Fbody%253D1%253A30%253A302%250Aq%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A8%253A276%2530Ae%2540%http2% 252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A30%253A123%250Ae%252F%253C%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%252Fangular.min.js%2533 253A181%250Aq%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A8%253A276%250Ae%2540http%253A%252F%252Flocalhost%253A352Fangular%253A352Fangular.min. 253Fbody%253D1%253A30%253A123%250A%2524b%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A33%253A238%250AZb%252Fc%2Flocalhost%250AZb%252Fc%2555 253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A18%253A437%250AZb%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D5401%253Ac9% 253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.m in.js%253Fbody%253D1%253A18%253A220%250A%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A202%253A125%250AjQuery.Callbacks%253A%%253A% 252F%252Flocalhost%253a3000%252fasset%252fjquery.js%253fbody%253d1%253a3120%253a1%250%253a1%250ajquery.cirewith%252fsf.firewith%2540http%253a%252f%252flocalhost%253a3000%252fassets%252fjquery.js%253fbody%253d1%253a3232% 253a7%250a.ready%2540http%253a%252f%252frocalhost%253a3000%252fassets%252fjquery.js%253fbody253d1%253a3444%253a3%250个合金%2540http%253a%252f%252flocalhost%253a3000%252fasset%252fjquery.js%253fbody%253fbody% 253D1%253A3475%253A3%250A%0AF%2F%3C%40http%3A%2F%2Flocalhost%3A3000%2Fassets%2Fangular.min.js%

我感觉 Restangular 甚至没有被使用...

编辑:Restangular 取决于 Lodash 或 Underscore!一旦我安装了 Lodash,它就可以工作了

【问题讨论】:

  • 从未使用过Restangular,所以这可能是一个细微差别,但是看一下文档,您对all 的使用似乎是相反的。不应该是Restangular.all('books')吗?
  • 是的,是的....但不幸的是,我仍然遇到同样的错误。关于该错误的文档说这意味着“无法实例化 AngulaRails 模块”这是我的特定 angularails 应用程序

标签: ruby-on-rails angularjs restangular


【解决方案1】:

你需要提供lodash,它是restangular的依赖

在 application.js 中

//= require lodash/dist/lodash.min
//= require angular/angular
//= require restangular/dist/restangular

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多