HOW TO GET THE RAILS VALUE IN ANGULAR JS (Connection between angular and rails) EASIEST WAY TO CONNECT ANGULAR JS TO RAILS:
If you want to add Angular Js to rails
gem 'angularjs-rails'
gem 'angularjs-rails-resource', '~> 1.1.1'
First gem is for Add the angular js frame work to your rails app.
Second gem is to use the rails routes in angular js.
put this inside of your gem file and add these commands inside the application.js
//= require lib/angular.min
//= require angularjs/rails/resource
Put these lines after jquery because jquery wants to boot first .
Above method you have to pass the object to the json and we can use the object key values directly ...
<script>
var json=
{
name:<%= @name.to_json.html_safe%>
marks:<%= @marks.to_json.html_safe%>
}
</script>
And here you can just pass the single rails variable inside the Angular controller... Then you can use the name and marks directly for the json variable ...
then you can start coding :-)
First of all you need to know about Angular js and Rails (MVC)
Go to this links to know About Angular JS and Rails:
This is the websites i learned how to make a program in above languages You can use any of websites to learn...
If you want to add Angular Js to rails
gem 'angularjs-rails'
gem 'angularjs-rails-resource', '~> 1.1.1'
First gem is for Add the angular js frame work to your rails app.
Second gem is to use the rails routes in angular js.
put this inside of your gem file and add these commands inside the application.js
//= require lib/angular.min
//= require angularjs/rails/resource
Put these lines after jquery because jquery wants to boot first .
Here you have to add these lines in your Angular controller
$scope.init_jsons = function(){
if(typeof json != 'undefined')
{
angular.forEach(json, function (value,key){
$scope[key] = value;
})
}
$scope.init_jsons();
then Add all rails variable to the script tag like this
<script>
var json=<%= @json.to_json.html_safe%>
</script>
Above method you have to pass the object to the json and we can use the object key values directly ...
<script>
var json=
{
name:<%= @name.to_json.html_safe%>
marks:<%= @marks.to_json.html_safe%>
}
</script>
And here you can just pass the single rails variable inside the Angular controller... Then you can use the name and marks directly for the json variable ...
then you can start coding :-)
No comments:
Post a Comment