将angular指令添加到rails中的simple_form会破坏集合输入的选定选项

dai*_*no3 6 simple-form angularjs ruby-on-rails-4

不确定是否有解决方法,我做错了,或者这是简单形式或角度之间的碰撞,但是:

.field
  = f.input :role, collection: roles.map(&:name), selected: user.role.try(:name), include_blank: 'Select', required: true, input_html: { "ng-model" => "role" }
Run Code Online (Sandbox Code Playgroud)

渲染(看起来正确):

   <select ng-model="role" class="select required" name="user[role]" id="user_role">
      <option value="">Select</option>
      <option value="system">system</option>
      <option selected="selected" value="fcm">fcm</option>
      <option value="regulatory">regulatory</option>
      <option value="operations">operations</option>
      <option value="help_desk">help_desk</option>
     </select>
Run Code Online (Sandbox Code Playgroud)

但所选值是include_blank"选择" 的值.是的,role是在用户上设置的.

Guy*_*Guy 3

这样做的原因是角度模型的工作方式,并且需要对此有所了解。本质上,Ruby 也会生成select正确选择selected选择框的选项。

但是,一旦加载了角度并看到ng-model了这一点select,它就会将当前选定的选项设置为模型的值,在您的情况下为role

因此,如果您想要为此建立角度模型select,则需要设置该模型的初始值。

请尝试以下操作:

.field
= f.input :role, collection: roles.map(&:name), include_blank: 'Select', required: true, input_html: { "ng-model" => "role", "ng-init" => "role = #{user.role.try(:name)}" }
Run Code Online (Sandbox Code Playgroud)

请注意,该selected选项已被完全删除,因为这将由 Angular 模型控制,该模型使用 ruby​​ 字符串插值初始化为该值。