Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My quest: a page of sliders, made with an ng-repeat through a JSON file. Spans showing the value of each slider. An input showing (value of the slider * a grams value per selected unit).

Here's my Plunkr with multiple sliders, each with a selection of 2 food values.

The span showing the output of the slider should initialise at 0. My first problem is, if I initialise it at 0 in the main controller as shown in a previous question, then this changes the scope of the slider demoVals.slider so that all sliders on the page now operate in unison:

 //assign initial values
  $scope.demoVals = {};
  $scope.demoVals.slider = 0;

If you comment out those lines, each slider can now be operated individually again.

I thought I could solve this by assigning a unique identifier to the ng-model & ng-bind of each slider & its output, but is there a better way to limit the scope so that the initialisation is happening individually for each ng-repeat?

My next problem is trying to call a multiplication function on the value of the output: how can I pass the grams unit selected (accessible in the template as {{ unit-grams }}) to the controller, and have the result returned as the value of the input?

   //calculate weight
   $scope.$watch('demoVals.slider', function (newVal) {
        if (typeof newVal !== 'undefined') {
          $scope.calculated = newVal * 3.14159; //an arbitrary number that I want to be the variable unit.grams
        } 
      });
share|improve this question

1 Answer 1

up vote 0 down vote accepted

I am not quite sure what you actually tried to do to solve this (what do you mean by "by assigning a unique identifier to the ng-model & ng-bind of each slider & its output"?), but if you want separate values for each food slider, it should also be an element of the food object as it belong to this object in nature.

Instead of using demoVals.slider use e.g. food.slider. You can also initialize this using ng-init, i.e. ng-init="food.slider=0;"

I updated the Plunkr: http://plnkr.co/edit/CbkSZpzoqXwcMkV1EDdp?p=preview

However, it seems like a good idea to separate all this into its own food controller. This way you could process each food item in its own scope.

share|improve this answer
    
Wow, great- thank you for the working example. Should I also move the ng-init="unit = food.unit[0]" to the ng-repeat, where you have moved the other initialisations in your demo? I can't seem to get the select lists to initialise with the first option selected now. –  Ila Nov 4 '13 at 12:54
    
Found it: food.unit.selected = food.unit[0]" in the ng-repeat. Thanks again for the working demo! –  Ila Nov 4 '13 at 13:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.