在轨AngularJS过滤器 [英] AngularJS Filter in rails

查看:167
本文介绍了在轨AngularJS过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做出on Rails应用程序红宝石,我的帖子的搜索列表。我有AngularJS应用程序的工作。所有的帖子都保存在@posts轨道。我怎么会让那多过滤AngularJS?
下面是相关的观点:

I'm trying to make a searchable list of posts on a ruby on rails application that I made. I have AngularJS working on the application. All of the posts are saved on rails in @posts. How would I make AngularJS filter over that? Here is the relevant view:

<h1>Posts</h1>
<div ng-app>
    <div ng-controller = "PostCtrl">
      <input placeholder = "Search Post Titles", ng-model="searchText">
        <div ng-repeat="post in posts | filter:searchText">
          <h2>{{post.title}}</h2>
          <p>{{post.text}}</p>
        </div>
    </div>
</div>

我不知道如何来填充对象的角度数组职位@posts。

I'm not sure how to fill the angular array posts with the objects in @posts.

推荐答案

持续达文特赖恩的答案,你可以试试,因为这。
第一次使用NG-的init =的init()在您的控制器:

Continuing the answer of Davin Tryon, you can try as this. First use ng-init="init()" in your Controller:

<div ng-controller = "PostCtrl" ng-init="init( <%= @posts.to_json %> )">

然后在您的控制器,你可以这样做:

Then in your controller you can do:

$scope.init = (posts) ->
    $scope.posts = angular.fromJson(posts)

再上岗将是它可以在你的范围访问的JavaScript对象,如果你有采用了棱角分明的资源进行查询。

Then posts will be a JavaScript Object which can be accessed in your scope as if you have query it using angular resource.

如果您要包括职位协会(可以说评论即),您可以检查<一href=\"http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json#method-i-as_json\"相对=nofollow>导轨文档为to_json(或as_json)和:include选项

If you want to include the associations of posts (lets say comments i.e) you can check rails docs for to_json (or "as_json") and the :include option

这篇关于在轨AngularJS过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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