我正在尝试将一些 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/