javascript - AngularJS calculate two dynamic values from an array -


i new web development , ran problem arrays in angularjs.

i have ng-repeat sequence. in sequence 2 sliders, repeated on , on due ng-repeat.

the corresponding html snippet looks this:

<h1>index = {{index}}</h1>   <div ng-repeat="alert in alerts">       <rzslider rz-slider-model="alert.value"               rz-slider-options="alert.options"></rzslider>     <br>     <br>     <div>       <h4>{{alert.weighttitle}}</h4>       <div>         <md-slider flex md-discrete ng-model="alert.weight" step="1" min="1" max="100" aria-label="rating"></md-slider>       </div>     </div>   </div> 

considering all links , scripts included , controller correctly set up.

the code above following:

  • it displays $scope.index angularjs controller.
  • two different sliders appear iterate through array called alerts. there takes values.

the interesting snippet of controller looks this:

$scope.alerts = [   { id: 1,     title: 'title1',     weighttitle: 'weighttitle1',     value: 1,     weight: 10,     options: {       showticks: true,       hidepointerlabels: true,       hidelimitlabels: true,       stepsarray: [         { value: 1, legend: '<10' },         { value: 2, legend: '<50' },         { value: 3, legend: '<250' },         { value: 4, legend: '<500' },         { value: 5, legend: '>500' }        ]     }   },   { id: 2,     title: 'title2',     weighttitle: 'weighttitle2',     value: 5,     weight: 1,     options: {       showticks: true,       hidepointerlabels: true,       hidelimitlabels: true,       stepsarray: [         { value: 1, legend: '<2mio' },         { value: 2, legend: '<10mio' },         { value: 3, legend: '<50mio' },         { value: 4, legend: '<100mio' },         { value: 5, legend: '>100mio' }        ]     }   },   { id: 3,     title: 'title3',     weighttitle: 'weighttitle3',     value: 1,     weight: 1,     options: {       showticks: true,       hidepointerlabels: true,       hidelimitlabels: true,       stepsarray: [         { value: 1, legend: '<5%' },         { value: 2, legend: '<10%' },         { value: 3, legend: '<15%' },         { value: 4, legend: '<20%' },         { value: 5, legend: '>20%' }        ]     }   },   { id: 4,     title: 'title4',     weighttitle: 'weighttitle4',     value: 3,     weight: 69,     options: {       showticks: true,       hidepointerlabels: true,       hidelimitlabels: true,       stepsarray: [         { value: 1, legend: '<5' },         { value: 2, legend: '<10' },         { value: 3, legend: '<15' },         { value: 4, legend: '<20' },         { value: 5, legend: '>20' }        ]     }   }, ]; 

the above snippet part of controller included in html correctly , therefore working.

my question how can set $scope.index getting each value , each weight array , multiplies values accordingly value * weight. it has noted user can change value changing manipulating sliders, correspond specific variable in array

i tried $watch command this:

$scope.$watch('alerts', function(newvalue, oldvalue){   $scope.index = oldvalue + (newvalue.weight * newvalue.value); }, true); 

but not working.

you can use $scope.$watch , listen changes , when ever changes happened can update view. can try use ng-change="update(alert)" function , pass value function.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -