我正在使用AngularJS和Rails,并按照http://start.jcolemorrison.com/setting-up-an-angularjs-and-rails-4-1-project/和其他几个教程在我的桌面上启动并运行一个非常简单的“Hello World”示例。问题是,当我将它部署到服务器时,基于Angular的代码都不再有效。相反,我在控制台中收到Uncaught ReferenceError: require is not defined
错误。
我意识到我的部分问题是浏览器不知道如何处理'require'函数调用,这是有道理的,因此我使用browserify-rails安装了Browserify。但是,它仍然不像我期望的那样在服务器上工作,我得到同样的错误。
链接到实际网站:Link
这是我的application.html.erb
(简化):
<!DOCTYPE html>
<html>
<head>
<base href="/" />
<title>ShibaInu</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => false %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => false %>
<%= csrf_meta_tags %>
</head>
<body ng-app="shibaInu">
<div class="container">1 + 2 = {{ 1 + 2 }}</div>
<div class="container">
<%= yield %>
</div>
</body>
</html>
index.html.erb
:
<div ng-view=""></div>
home.html.erb
(在容器内呈现):
<h1>The Home View!</h1>
<ul>
<li ng-repeat="thing in things">
{{thing}}
</li>
</ul>
home.js
:
angular.module('shibaInu')
.controller('HomeCtrl', function ($scope) {
$scope.things = ['Angular', 'Rails 4.1', 'Working', 'Together!!'];
});
app.js
:
angular
.module('shibaInu', [
'ngRoute',
'templates'
]).config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.html',
controller: 'HomeCtrl'
});
$locationProvider.html5Mode(true);
});
application.js
//= require jquery
//= require jquery_ujs
//= require angular
//= require angular-route
//= require angular-resource
//= require angular-rails-templates
//= require bootstrap-sprockets
//= require ng-app/app.js
//= require_tree ../templates
//= require_tree .
答案 0 :(得分:0)
我做错了几件事,但我会试着总结一下:
angularjs-rails
宝石。我把它添加到我的项目中。新的home.js:
angular.module('shibaInu')
.controller('HomeCtrl', ['$scope', function ($scope) {
$scope.things = ['Angular', 'Rails 4.1', 'Working', 'Together!!'];
}]);
新app.js:
angular
.module('shibaInu', [
'ngRoute',
'templates'
]).config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.html',
controller: 'HomeCtrl'
});
$locationProvider.html5Mode(true);
}]);