简体   繁体   English

Angular.js与Ruby On Rails Forms集成

[英]Angular.js integration with Ruby On Rails Forms

Using angular.js often items like ng-click or ng-model are written directly in the html form element like so.... 使用angular.js经常像ng-click或ng-model这样的项目直接写在html表单元素中,就像这样....

<input type="text" ng-model="name">

How would I do that with rails? 我怎么用铁轨呢? As rails uses embedded ruby and generates the html.... 因为rails使用嵌入式ruby并生成html ....

<%= form_for(@user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>

How would I add the ng-model to <%= f.text_field :name %> ? 如何将ng-model添加到<%= f.text_field:name%>?

Ideally, you don't want to be mixing embedded ruby interpolation and Angular's interpolation. 理想情况下,您不希望混合嵌入式红宝石插值和Angular插值。 It's better to have ruby asynchronously serve JSON to Angular, and let Angular take care of filling in the data on the client side. 最好让ruby异步地为Jular提供JSON,让Angular负责填写客户端的数据。

试试这个,当它是一个连字符分隔的单词时,你需要放入一个哈希表示法。

f.text_field :name, :ng => {:model => "name"}

I was working with AngularJS directives and Rails 4 in order to make the bootstrap-datepicker jquery plugin work on a Rails 4 erb template, the code that I used inside the text_field_tag is the following: 我正在使用AngularJS指令和Rails 4,以使bootstrap-datepicker jquery插件在Rails 4 erb模板上工作,我在text_field_tag使用的代码如下:

<%= text_field_tag(:start_day, nil, class: 'form-control', datepicker: 'datepicker') %>  

It's important to notice that this works with AngularJS directives, the code that you get on the DOM is as follows: 重要的是要注意这与AngularJS指令一起使用,您在DOM上获得的代码如下:

<input class="form-control" datepicker="datepicker" type="text">

I worked with the directive in the following way: 我以下列方式使用该指令:

timeAdminCal.directive('datepicker', function(){
      return {
        restrict: 'A',
        link: function ($scope, $element) {
          $element.datepicker({
            format: 'd/m/yyyy',
            autoclose: true,
            todayHighlight: true
          });
        }
      }
    });

Notice that, according to AngularJS directive docs you can restrict a class name, so you may use any class name on your text_field_tag and it will work too. 请注意,根据AngularJS指令文档,您可以restrict类名,因此您可以在text_field_tag上使用任何类名,它也可以使用。

timeAdminCal.directive('datepicker', function(){
      return {
        restrict: 'C', // Bind DOM element by class name 'datepicker'
        link: function ($scope, $element) {
          $element.datepicker({
            format: 'd/m/yyyy',
            autoclose: true,
            todayHighlight: true
          });
        }
      }
    });

以下是对我有用的(但它将禁用angularjs的错误消息(“请输入名称。”),即required: "required" ):

<%= f.input :id, as: :select, label: false, prompt: 'Select a selection',  input_html: { "ng-model" => ""} %>

I think you can use the :html option to set any element attributes. 我认为你可以使用:html选项来设置任何元素属性。 Haven't tried it with angular.js special attributes though ;-) 有没有尝试过angular.js特殊属性;-)

f.text_field :name, :html => { :ng-model => "name" }

On my current project, I had to start transforming static templates into angular pages, what I did was to render jbuilder views inside the template and put it in ng-init. 在我当前的项目中,我不得不开始将静态模板转换为角度页面,我所做的是在模板中呈现jbuilder视图并将其放在ng-init中。

If the screen ever becomes part of a single page app, I will simply have to remove that render and add a query to the api to load that data. 如果屏幕曾经成为单页面应用程序的一部分,我只需删除该渲染并向api添加查询以加载该数据。 That's how Twitter does it, and it's simple and effective. Twitter就是这样做的,它简单而有效。

If you use ng-model, this means that you overwrite the form_for behavior for objects. 如果使用ng-model,则表示您覆盖了对象的form_for行为。 For example, if you have 例如,如果你有

= form_for :user do |f|
  = f.text_field :username, 'ng-model': user.username

This means the text field will not render value passed from rails controller. 这意味着文本字段不会呈现从rails控制器传递的值。 You need to add the value of username to angular model inside the angular controller. 您需要将用户名的值添加到角度控制器内的角度模型中。

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

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