javascript - 如何在 rails 中初始化 Angularjs ng-model

标签 javascript ruby-on-rails ruby angularjs erb

关于在 rails erb 文件中使用 angularjs 双向绑定(bind)的问题。

假设我的 .erb 文件中的输入值有一个原始值,即

例子.erb

<input type="text" value=" <%= @item.title %> " ng-model ="item.title">

正如您在上面的示例中注意到的那样,输入也与 angularJs 模型绑定(bind)。 例子.js

mayApp.controller('newItemController', function itemController($scope) { 
   $scope.item = {title:"angularJs model value", price: 1000}
}

我发现原来的@item.title 被angularJs 模型值覆盖了。 但是,我希望事情以相反的方式发生,也就是说,angular js 模型是由 .erb 文件中的值初始化的。我该怎么做?

我尝试将 example.js 放入 Assets 管道。 IE。 例子.js.erb

mayApp.controller('newItemController', function itemController($scope) { 
   $scope.item = {title:"<%= @item.title %>", price: 1000}
}

但@item 在管道中始终为 nil。我想 @item 只能在 View 中使用?

最佳答案

您可以为此使用 ng-init:

<input type="text" ng-init="item.title = '<%= @item.title %>'" ng-model ="item.title">

关于javascript - 如何在 rails 中初始化 Angularjs ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20302166/

相关文章:

ruby-on-rails - 未初始化常量 ActionController::Caching::Sweeper

ruby-on-rails - rails : Resetting form fields with page refresh

ruby-on-rails - Rails 范围 : association is nil

ruby-on-rails - 我如何设置 'attr_accessible' 以不允许访问使用 Ruby on Rails 的模型的任何字段?

带有新创建行的 Javascript tr click 事件(更新 css)

javascript - 在每个表格单元格中选择下拉菜单后将其删除

ruby - 为什么此命令在此特定代码中不起作用?

作为方法调用的 Ruby 类

javascript - 使用 Google Polymer 绑定(bind)子属性

javascript - iframe 中的页面宽度