加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 在rails中为simple_form添加angular指令会破坏集合

发布时间:2020-12-17 07:01:16 所属栏目:安全 来源:网络整理
导读:不确定是否有解决方法,我做错了,或者这是简单形式或角度之间的碰撞,但是: .field = f.input :role,collection: roles.map(:name),selected: user.role.try(:name),include_blank: 'Select',required: true,input_html: { "ng-model" = "role" } 渲染(看起来
不确定是否有解决方法,我做错了,或者这是简单形式或角度之间的碰撞,但是:

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

渲染(看起来正确):

<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>

但是所选值是’Select’的include_blank值.是的,角色是在用户上设置的.

解决方法

它之所以这样做是因为角度模型的工作方式,需要对此有所了解.基本上,ruby生成选择正确选择选择框的选定选项.

但是,一旦加载角度并在此选择上看到ng-model,它会将当前选定的选项设置为模型的值(在您的情况下为角色).

因此,如果要为此选择设置角度模型,则需要设置该模型的初始值.

请尝试以下方法:

.field
= f.input :role,input_html: { "ng-model" => "role","ng-init" => "role = #{user.role.try(:name)}" }

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读