1

I have 2 inputs here, one is setting the value using scope and the other is setting the value after the setTimeout, but my problem here is that... it doesn't show the value immediately and I need to click the second input to show the value. Any solution for this?

angular.module('selectExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.firstInput = "click the second input";
    $scope.secondInput;
    
    function init(){
     setTimeout(function(){ 
     $scope.secondInput = "second input";
     }, 1000);
    } 
    init();
  }]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="selectExample" ng-controller="ExampleController">
<input type="text" ng-model="firstInput"></input>
<input type="text" ng-model="secondInput"></input>
</div>

3 Answers 3

4

Use $timeout instead of setTimeout

angular.module('selectExample', [])
  .controller('ExampleController', ['$scope', '$timeout', function($scope, $timeout) {
    $scope.firstInput = "click the second input";
    $scope.secondInput;
    
    function init(){
     $timeout(function(){ 
       $scope.secondInput = "second input";
     }, 1000);
    } 
    init();
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="selectExample" ng-controller="ExampleController">
  <input type="text" ng-model="firstInput"></input>
  <input type="text" ng-model="secondInput"></input>
</div>

AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc.

For more information, see

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

1 Comment

oh thanks haha i didn't know the difference between these two
1

First of all you should follow the solution given by @barbsan. using $timeout instead of setTimeout. That being said you'd still want to $scope.$apply().

to do that if you look into angularjs docs for $timeout, the third parameter is for that. so You can just do the following

$timeout(function() {
              $scope.secondInput = "second input";
            }, 1000,true);

1 Comment

But third parameter is true by default, so you can omit it
0

When you update the second input from setTimeout, the change isn't detected, and you must force change detection manually.

I think adding $scope.$digest(); after $scope.secondInput =... should do the trick.

1 Comment

$digest only processes the watchers of a scope and its children. If those watchers change scope properties of parent scopes, the parent watchers will not be processed. This can create weird bugs. It is better to replace setTimeout with the $timeout service.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.