0

Range should change its value and position depending on the param and has not changed to change the param

HTML:

  <body  ng-app="myApp">
    <div ng-controller="MyRngCtrl">
      Param:{{param}}
    </div>
    <hr>
    <div ng-controller="rangeCtrl">
    <div>modelValue:{{modelValue}}</div>
      <input type="range" style="width: 300px;"
             min = "0"
             max = "300"
             step = "1"
             ng-change = "rangeChange()"
             ng-model = "modelValue">
    </div>
  </body>

JS:

app.controller('rangeCtrl', function($scope) {

    $scope.modelValue =  $scope.param;
    $scope.rangeChange = function() {
    }
});

Example: JSFiddle

3
  • Your example here does not represent your fiddle. Then, you are using two different controllers and then you are updating a value outside of the digest-cycle of angularjs. Commented Dec 7, 2016 at 10:16
  • @bash.d in the original problem, my controller looks like that: JSFibble Commented Dec 7, 2016 at 10:26
  • In the first example, the controller "MyRngCtrl" simply "simulates" value param Commented Dec 7, 2016 at 10:32

2 Answers 2

1

Try this

var app = angular.module('myApp', []);
app.controller('MyRngCtrl', function($scope, $timeout) {

  (function update() {
    $timeout(update, 5000);
    $scope.param = Math.round((Math.random() * 10) * 10 * 3);
    $scope.modelValue = $scope.param

  }());
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <div ng-controller="MyRngCtrl">
    Param:{{param}}

    <hr>

    <div>modelValue:{{param}}</div>
    <input type="range" style="width: 300px;" min="0" max="300" step="1" ng-change="rangeChange()" ng-model="modelValue">
  </div>
</body>

Sign up to request clarification or add additional context in comments.

1 Comment

This solution works, but your callback logic is tied to the timeout handler, this is not very good style.
0

I have one more Answer using $rootScope

var app = angular.module('myApp', []);
app.controller('MyRngCtrl', function($rootScope, $timeout) {
  (function update() {
    $timeout(update, 5000);
    $rootScope.param = Math.round((Math.random() * 10) * 10 * 3);
  }());
});
app.controller('rangeCtrl', function($scope, $rootScope, $timeout) {
  (function update() {
    $timeout(update, 5000);
    $scope.modelValue = $rootScope.param;
  }());
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <div ng-controller="MyRngCtrl">
    Param:{{param}}
  </div>
  <hr>
  <div ng-controller="rangeCtrl">
    <div>modelValue:{{modelValue}}</div>
    <input type="range" style="width: 300px;" min="0" max="300" step="1" ng-model="modelValue">
  </div>
</body>

2 Comments

It looks great! I went to the other side: JSFiddle, and faced with the fact that I can not change "modelValue" at least for a while, until the value of "Param" not changed
if you reduce the period of "$ timeout" then there is the same problem will

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.