# View not updating after changing $scope variable

**URL:** <https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092>\
**Category:** ionic-v1\
**Created:** [January 5, 2015, 10:31am UTC](https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092 "2015-01-05T10:31:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cys](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@cys](https://forum.ionicframework.com/u/cys)\
**Post date:** [January 5, 2015, 10:31am UTC](https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092/1 "2015-01-05T10:31:36Z")

</div>

To simplify the problem I have:

.controller(‘DashCtrl’, function($scope) {  
$scope.message = “a”;  
console.log($scope.message);  
setTimeout(function() {  
$scope.message = “b”;  
console.log($scope.message)  
}, 8000);  
})

The first console.log($scope.message) prints out "a"  
The second console.log($scope.message) prints out "b"  
However the view remains unchanged showing “a”

What is the issue? Is the second $scope.message assignment creating a different namespace?

How do I get the view to update?

---

<div class="post-metadata">

**Author:** ![cys](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@cys](https://forum.ionicframework.com/u/cys)\
**Post date:** [January 5, 2015, 10:46am UTC](https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092/2 "2015-01-05T10:46:12Z")

</div>

I managed to solve it after reading this article:

[http://jimhoskins.com/2012/12/17/angularjs-and-apply.html](http://jimhoskins.com/2012/12/17/angularjs-and-apply.html)

Thank you Mr Hoskins.

I’ll just leave this here for anyone else with the same problem.

---

<div class="post-metadata">

**Author:** ![devqon1](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@devqon1](https://forum.ionicframework.com/u/devqon1)\
**Post date:** [January 5, 2015, 11:59am UTC](https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092/3 "2015-01-05T11:59:10Z")

</div>

You could do it without $apply by defining a global object that holds the variables instead of a global variable:

```
.controller('DashCtrl', function($scope) {
    $scope.data = { message: "a" };
    console.log($scope.data.message);
    
    setTimeout(function() {
        $scope.data.message = "b";
        console.log($scope.data.message)
    }, 8000);
})

```

Also, I suggest you **do not** use `setTimeout`, but use `$timeout` instead, because `setTimeout` ignores the Angular digest cycle:

```
.controller('DashCtrl', function($scope, $timeout) {

    //..
    $timeout(function() {
        //..
    }, 8000);
}
```

---

<div class="post-metadata">

**Author:** ![cys](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@cys](https://forum.ionicframework.com/u/cys)\
**Post date:** [January 5, 2015, 12:33pm UTC](https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092/4 "2015-01-05T12:33:19Z")

</div>

Thanks so much 😄

The actual code that I am using to trigger a change to the $scope after a [Socket.IO](http://Socket.IO) event is received. In this instance, $apply seems to work (though not the neatest code), I couldn’t get it to work with the global object.

.controller(‘DashCtrl’, function($scope) {  
msg=“start”  
$scope.message = msg;  
console.log($scope.message);  
socket.on(‘msgin’, function (data) {  
msg =data.msg;  
$scope.$apply(function(){$scope.message = msg;});  
});  
})

---

<div class="post-metadata">

**Author:** ![Silfide](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@Silfide](https://forum.ionicframework.com/u/Silfide)\
**Post date:** [September 18, 2015, 3:55pm UTC](https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092/5 "2015-09-18T15:55:20Z")

</div>

Sorry for necromancer, but I though it was good to clear this point.

I can’t see global variables in your example. I think global variables are made with $rootScope. Maybe you meant another thing. I just point it because maybe someone come here and get confused with global/local variables.

---

<div class="post-metadata">

**Author:** ![hermanho237](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@hermanho237](https://forum.ionicframework.com/u/hermanho237)\
**Post date:** [August 22, 2016, 2:57pm UTC](https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092/6 "2016-08-22T14:57:37Z")

</div>

> [@cys](#):
>
> I managed to solve it after reading this article:
> 
> [AngularJS and scope.$apply — Jim Hoskins](http://jimhoskins.com/2012/12/17/angularjs-and-apply.html)
> 
> Thank you Mr Hoskins.
> 
> I’ll just leave this here for anyone else with the same problem.

Thanks @cys . That saved my day

---

<div class="post-metadata">

**Author:** ![junaid0098](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@junaid0098](https://forum.ionicframework.com/u/junaid0098)\
**Post date:** [November 15, 2016, 6:41am UTC](https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092/7 "2016-11-15T06:41:17Z")

</div>

thanks man.!  
i was facing that problem routinely…

---

<div class="post-metadata">

**Author:** ![sheikhwasim](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sheikhwasim/32/117246_2.png) [@sheikhwasim](https://forum.ionicframework.com/u/sheikhwasim)\
**Post date:** [December 27, 2018, 11:43am UTC](https://forum.ionicframework.com/t/view-not-updating-after-changing-scope-variable/15092/8 "2018-12-27T11:43:39Z")

</div>

Simply awesome, what I was looking it was exact result.
