简体   繁体   English

在AngularJ中使用部分视图的最佳方法是什么(在Rails中)?

[英]What is the best way to use partial view for AngularJs (in Rails)?

Suppose there is a form page for Project , which has name and description . 假设有一个Project的表单页面,其中有namedescription Take a look at the render :partial . 看一下render :partial

<div class="page-header">
  <h2>{{project.id ? 'Edit ' : 'Add new'}} project</h2>
</div>

<div class="row">
  <div class="col-xs-6 col-xs-offset-2">
    <form class="form-horizontal">
      <%= render :partial => 'templates/forms/text', :object => 'name', :locals => {:model => 'project'} %>
      <%= render :partial => 'templates/forms/text', :object => 'description', :locals => {:model => 'project'} %>
      <div class="form-group">
        <div class="col-xs-offset-3 col-xs-9">
          <button ng-click="saveProject()" type="submit" class="btn btn-success">Submit</button>
          <a ng-href="{{ path('UserIndexCtrl') }}" class="btn btn-default">Back</a>
        </div>
      </div>
    </form>
  </div>
</div>

And here is a partial being used. 这是部分使用的方法。

<div class="form-group" ng-class="{'has-error': errors.<%= text.underscore %>}">
  <label for="<%= model %>.<%= text %>" class="col-xs-3 control-label"><%= text.titleize.humanize %></label>
  <div class="col-xs-9">
    <input id="<%= model %>.<%= text %>" name="<%= model %>.<%= text %>" type="<%= local_assigns[:type] || 'text' %>" class="form-control" ng-class="{'text-danger': true}" ng-model="<%= model %>.<%= text %>">
    <span class="help-block" ng-show="errors.<%= text.underscore %>"><%= text.titleize.humanize %> {{errors.<%= text.underscore %>.join(', ')}}</span>
  </div>
</div>

I am not sure this is the proper way to do a partial rendering in Rails + AngularJs combo. 我不确定这是在Rails + AngularJs组合中进行部分渲染的正确方法。 Wonder if there's a more angular-ish way? 想知道是否还有更棱角分明的方式? Thanks! 谢谢!

I would use a Helper to generate the input instead of writing directly in the HTML, something like: 我将使用Helper来生成输入,而不是直接在HTML中编写,例如:

def input_for_model(model_name, text, options = { }) # I actually have no idea what is doing your input, you can probably find a better name
  options = { id: "#{model_name}.#{text}", name: "#{model_name}.#{text}",
              type: 'text', class: 'form-control', 
              ng-class: "{'text-danger': true}", 
              ng-model: "#{model_name}.#{text}" 
            }.merge(options)
  tag(:input, options)
end

And use it like this: 并像这样使用它:

<div class="form-group" ng-class="{'has-error': errors.<%= text.underscore %>}">
  <label for="<%= model %>.<%= text %>" class="col-xs-3 control-label"><%= text.titleize.humanize %></label>
  <div class="col-xs-9">

    <%= input_for_model(model, text, type: local_assigns[:type] || 'text') %>

    <span class="help-block" ng-show="errors.<%= text.underscore %>"><%= text.titleize.humanize %> {{errors.<%= text.underscore %>.join(', ')}}</span>
  </div>
</div>

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

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