简体   繁体   English

将Angular.js与Rails集成

[英]Integrating Angular.js with Rails

Background 背景

I'm getting started with building an app with an Angular.js front end and a Rails back end. 我开始使用Angular.js前端和Rails后端构建应用程序。 Working through the thinkster tutorial, I believe I just need bower to help me install the angular and bootstrap packages (basically the front end dependencies). 通过thinkster教程,我相信我只需要Bower即可帮助我安装angular和bootstrap软件包(基本上是前端依赖项)。 Then I need to require the packages through the asset pipeline in Rails, use a few additional gems, and I should be good to go. 然后,我需要通过Rails中的资产管道要求这些软件包,使用一些额外的gem,这应该很好。

Problem 问题

To see if things are working properly, I have a very simple _home.html file in app/assets/javascripts/home and that should be loaded up through ui-router under the 'home' state (ui-router configuration is in app.js). 为了查看一切是否正常,我在app / assets / javascripts / home中有一个非常简单的_home.html文件,应通过ui-router在“ home”状态下加载该文件(ui-router配置在应用程序中)。 js)。 The end result should be a page that displays "ScholarShipIt" and "Home Template" when the ui-view state is loaded correctly. 最终结果应该是正确加载ui视图状态时显示“ ScholarShipIt”和“主页模板”的页面。 However, it just displays "ScholarShipIt" which tells me that the state isn't loading. 但是,它仅显示“ ScholarShipIt”,告诉我状态未加载。

Troubleshooting 故障排除

I have tried outputting text to the console at various places in the js files to see where things are loading. 我尝试将文本输出到js文件中各个位置的控制台,以查看加载的位置。 I've also tried setting $scope.test in the app.js file and then setting up an expression to display that value but that did not work either. 我也尝试过在app.js文件中设置$ scope.test,然后设置一个表达式来显示该值,但这也不起作用。 My impression is that the angular packages are not being loaded up properly and I'm not sure what I'm missing. 我的印象是,角度包没有正确加载,我不确定自己缺少什么。

Code Samples 代码样例

Relevant gems used 所使用的相关宝石

gem 'angular-rails-templates'
gem 'angular-ui-bootstrap-rails'
gem 'angular_rails_csrf'
gem 'responders'
gem 'sprockets'

application.js application.js

//= require angular
//= require angular-rails-templates
//= require angular-ui-router
//= require angular-ui-bootstrap
//= require_tree .

app.js app.js

(function(){
  var app = angular.module('scholarShipIt', ['ui.router','templates']);

  app.config(function($stateProvider, $urlRouterProvider){

    $stateProvider
      .state('home', {
      url: '/home',
      controller: 'HomeController',
      templateUrl: 'home/_home.html'
    });

  $urlRouterProvider.otherwise('home');
  });

}());

HomeController.js HomeController.js

(function() {
  var app = angular.module('scholarShipIt');

  var HomeController = function($scope) {
    $scope.test = "testing";
  };
  console.log("test");

  app.controller('HomeController', HomeController);
}());

Main Question 主要问题

What am I missing from my application that Rails needs to load the angular files? 我的应用程序中缺少什么,Rails需要加载角度文件?

(I'd be curious to know how you usually set up your application for this as well) (我很想知道您通常也如何为此设置应用程序)

GitHub Repo GitHub回购

我的javascript_include_tag在“申请”后出现了一些错误的文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM