how to initialize Angularjs ng-model in rails
angularjs, erb, javascript, ruby, ruby-on-rails
Solution
You can use `ng-init` for this:
<input type="text" ng-init="item.title = '<%= @item.title %>'" ng-model ="item.title">
Problem
A question about using angularjs two way binding and in rails erb files. Suppose value of input in my .erb file has an original value i.e. example.erb ``` <input type="text" value=" <%= @item.title %> " ng-model ="item.title"> ``` As you may notice in above example, the input is also binded with a angularJs model. example.js ``` mayApp.controller('newItemController', function itemController($scope) { $scope.item = {title:"angularJs model value", price: 1000} } ``` I found that the original @item.title is overridden by the angularJs model value. However, I want things happen in the opposite way, that is, the angular js model is initialised by value from .erb file. How can I do that? I have tried to put example.js into asset pipeline. i.e. example.js.erb ``` mayApp.controller('newItemController', function itemController($scope) { $scope.item = {title:"<%= @item.title %>", price: 1000} } ``` but the @item is alway nil in the pipeline. I suppose the @item is only available in views?