Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:37:58+00:00 2026-06-15T08:37:58+00:00

I come from a Rails background and am attempting to use AngularJS with Rails

  • 0

I come from a Rails background and am attempting to use AngularJS with Rails. I am stuck on a very simple thing: how does one construct what rails calls ‘virtual attributes’ in an Angularjs environment? Let me give an example.

I have a rails model, called Medication, and it has two attributes, dosage and count. I want to have a ‘total’ virtual attribute that returns dosage * count. This is trivial in Rails, since it is just an instance method of class Medication.

So, how does that map into the AngularJS world?

Clarification: I am using an ng-repeat directive on Medications (plural) for which I have a MedicationsController. For each ng-repeat loop, I get a ‘medication‘ object, where I want to apply this ‘instance method’

total = function() { return dosage * count }

How is that coded? Where do I put the function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:37:59+00:00Added an answer on June 15, 2026 at 8:37 am

    It is really simple with AngularJS as you can use any JavaScript function in AngularJS view. So just define a function on your object (or directly on a scope). For example, give a controller like this:

    var MedicationCtrl = function($scope) {    
        $scope.medication = {
            dosage : 0.5,
            count : 10,
            total : function() {
                return this.dosage * this.count;
            }
        };
    }​
    

    You can simply write:

    Total: {{medication.total()}}
    

    Here is a jsFiddle: http://jsfiddle.net/4r5g5/1/

    Then, if you’ve got a collection of items and don’t want to put any logic on an object level, you can define this method on a controller like so:

    var MedicationCtrl = function($scope) {    
        $scope.total = function(medication) {
          return medication.dosage * medication.count;
        };
    };
    

    and use this function from a markup like follows:

    <ul ng-repeat="m in medications" ng-controller="MedicationCtrl">
        <li>{{m.dosage}} + {{m.count}} = {{total(m)}}</li>
    </ul>
    

    And the above code in the jsFiddle: http://jsfiddle.net/4r5g5/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Django, and I come from a rails background. As such, I'm asking
I come from a PHP/Rails background where deploying a website often means FTP/Checkout of
I come from a Java background, and am very used to wrapping code into
I come from an MVC background (Flex and Rails) and love the ideas of
I come from a Rails background and I'm having problems wrapping my head around
I come from a background in Ruby on Rails. Implementing search is relatively trivia
I come from a .NET background and just learned Ruby On Rails. My company
I come from a Java background and am getting more into .NET, what are
I come from a C++ background and I've been working with C# for about
I come from the asp.net world where we'd use an objectdatasource, hooked up to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.