AngularJS如何将值转换为整数?

时间:2015-06-04 03:30:20

标签: ruby-on-rails angularjs ruby-on-rails-4

我使用的是嵌套表单和AngularJS,我的代码就是这样。

#app/view/customer_templates/edit.html.erb
<%= f.simple_fields_for :customer_template, @customer.customer_template.build, child_index: '{{$index}}' do |t| %>
  <%= t.select :field_type_id, options_from_collection_for_select(FieldType.all, :id, :name, '{{customer_template.field_type_id}}') %>      
<% end %>

我的选择正在运行但未获得field_type_id的当前数据。

来自angular的

{{customer_template.field_type_id}}正在返回正确的数据,但它是一个字符串,当我尝试将其转换为{{customer_template.field_type_id}}.to_i之类的整数时,结果为 0 。我也尝试将它传递给变量并转换它,但我得到的结果相同。

我也尝试了这个{{parseInt(customer_template.field_type_id)}},并在我的angularjs $scope.parseInt = parseInt中尝试但没有运气。

这是我的问题,如下。

enter image description here

我必须有1-Checkbox,2-Downdown(是/否)和2-Textbox,你可以帮我这个吗?我是AngularJS的菜鸟。我唯一的问题是价值,我会把它放在options_from_collection_for_select里面。如果我可以放一个整数,它就可以了。

1 个答案:

答案 0 :(得分:1)

您是否尝试过javascript中的短手转换?

{{+customer_template.field_type_id}}

在你的控制器中这样:

$scope.parseInt = +parseInt;