关于在 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/