P
P
Pavel Shvedov2014-08-12 11:15:11
JavaScript
Pavel Shvedov, 2014-08-12 11:15:11

How to forward model in ng-click callback?

Hello! The situation is the following. I am writing a form for setting the parameters of some object in AngularJS. It has several sliders, which, in addition to the slider, have "+" and "-" buttons, which, respectively, should increment and decrement the value of the model to which the slider is attached. There are N such sliders, and I want to add the less() function to ng-click, which is the same for all "-" buttons, and the more() function, which is the same for all "+" buttons. I want to pass the model as parameters to these functions, and the minimum for the less() function, and the maximum for the more() function, since each slider has different ranges of values.
The values ​​managed by this slider lie in one object, and are its fields, for example:

$scope.results = {
  sliders: {
    slider_1: X,
    slider_2: Y,
    slider_3: Z
  }
}

Is it possible to somehow pass a pointer to a specific field of the results object, to change it inside the ng-click callback on the button? I tried just passing:
<a ng-click="less(results.sliders.slider_1, 0)"> - </a>

but in this case it is passed by value, and when it is changed, nothing happens in the object itself

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Romanov, 2014-08-12
@mmmaaak

You can try like this
A in the less method

$scope.less = function(name, num) {
    $scope.sliders[name];
}

_
_ _, 2014-08-12
@AMar4enko

It’s better then to make a prototype for the slider with the less and more methods, specify the minimum and maximum values ​​\u200b\u200bin the constructor and call less and more of a specific instance. And this is some kind of perversion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question