的Rails应用程序内AngularJS意见位置 [英] Location of AngularJS views within Rails app

查看:123
本文介绍了的Rails应用程序内AngularJS意见位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个Rails应用程序中集成AngularJS。我有一个控制器的书籍和书籍模型Rails应用程序。在Rails的索引视图我都这样了,AngularJS应该从这里拿过来:

  .page_text {NG-应用=> 图书}
  。行
    .twelve.columns
      DIV%{NG视图=> }

而AngularJS控制器看起来是这样的:

 函数BooksOverviewCtrl($范围,$ HTTP){
  $ http.get('/ books.json')。成功(功能(数据){
    $ scope.books =数据;
  });
}
。BooksOverviewCtrl $注射='$范围,$ HTTP'];

这是routeProvider:

  angular.module('书',[])。
  配置(['$ routeProvider',函数($ routeProvider){
  $ routeProvider。
      当('/书',{templateUrl:书籍/图书overview.html.haml',控制器:BooksOverviewCtrl});
}]);

目前,该AngularJS的观点是在应用程序/资产/ Java脚本/ angularjs /书籍/图书overview.html.haml。当我加载网页,它说找不到网页,所以我在哪里都存放在我的Rails应用程序的AngularJS看法?这是错误:

  GET HTTP://本地主机:3000 /书籍/图书overview.html.haml 404(未找到)


解决方案

一些研发后,我发现有两种方式来处理这个问题。第一个选项是既马赫布卜和Adnan多利安提的方式:存储在公共文件夹中的文件,因为它们是staic HTML文件反正。

虽然这个作品,感觉不自然适合我,因为当时的文件遭到散。有些是在公共文件夹,别人都在资产/ JavaScript的文件夹中。不是我的preferred选项,但它可能会为其他人工作。

第二个选项是将路径保存在一个特定的文件,如资产/ Java脚本/ routes.js.erb。因为.erb它是可以使用的文件中的Rails asset_path和创建这样的相对路径:

 时,('/书',{templateUrl:<%= asset_path('书overview.html.haml')%GT;,控制器:BooksOverviewCtrl});

也许不是完美的解决方案,但对我来说这效果最好,因为现在我所有的角文件都保存在一起资产/ JavaScript的文件夹中。

I'm trying to integrate AngularJS within a Rails app. I have a Rails app with a books controller and book model. In the Rails index view I have this, and AngularJS should take over from here:

.page_text{"ng-app" => "books"}
  .row
    .twelve.columns
      %div{"ng-view" => ""}

And the AngularJS controller looks like this:

function BooksOverviewCtrl($scope, $http) {
  $http.get('/books.json').success(function(data) {
    $scope.books = data;
  });
}
BooksOverviewCtrl.$inject = ['$scope', '$http'];

And this is the routeProvider:

angular.module('books', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/books', { templateUrl: 'books/book-overview.html.haml',   controller: BooksOverviewCtrl });
}]);

Currently, the AngularJS view is in "app/assets/javascripts/angularjs/books/book-overview.html.haml". When I load the page, it says page not found, so where do I have to store the AngularJS views in my Rails app? This is the error:

GET http://localhost:3000/books/book-overview.html.haml 404 (Not Found)

解决方案

After some researching, I found there are two ways to deal with this. The first option is the way both Mahbub and Adnan Doric mention: store the files in the public folder because they are staic html files anyway.

Although this works it feels not natural for me, because then the files get scattered. Some are in the public folder, and others are in the assets/javascripts folder. Not my preferred option, but it might work well for other people.

The second option is to store the routes in a specific file, like "assets/javascripts/routes.js.erb". Because of the ".erb" it is possible to use the Rails asset_path within the file and create a relative path like this:

when('/books', { templateUrl: "<%= asset_path('book-overview.html.haml') %>",   controller: BooksOverviewCtrl });

Maybe not the perfect solution, but for me this works best, because now all my Angular files are kept together within the "assets/javascripts" folder.

这篇关于的Rails应用程序内AngularJS意见位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆