Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular $scope variable not updating

In my angular, I define a scope variable $scope.letter_content. When the view is loaded, I load string from my database and set it to $scope.letter_content. Then, I populate on a texteditor(Froala) i'm using.

Below is the code for the view:

    {{letter_content}}
    <div ng-if="formData['page_number'] == 1 ">
        {{letter_content}}
        <textarea id="froala-sample-2" froala="froalaOptions" ng-model="letter_content"></textarea>
    </div>

So basically I set letter_content as ng-model for the texteditor. So when I make changes on the texteditor, it modifies the value $scope.letter_content.

One thing I found it weird is that when I modify the text in the texteditor, it changes {{letter_content}} inside the div. However, it does not update {{letter_content}} outside the div.

When I'm done editing the text in my texteditor, I send send a put request to update the value in the database with $scope.letter_content. However, it ends up sending {{letter_content}} outside the div which ends up not updating the content.

Why is this weird thing happening?

like image 570
JoHksi Avatar asked Aug 04 '16 09:08

JoHksi


People also ask

How does $scope work in angular?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).

Is $scope still supported in angular2?

In Angular 2.0, there will be no $scope .

What is the difference between $scope and scope?

The $ in "$scope" indicates that the scope value is being injected into the current context. $scope is a service provided by $scopeProvider . You can inject it into controllers, directives or other services using Angular's built-in dependency injector: module.

What is $scope vs this?

"How does this and $scope work in AngularJS controllers?" When the controller constructor function is called, this is the controller. When a function defined on a $scope object is called, this is the "scope in effect when the function was called". This may (or may not!) be the $scope that the function is defined on.


1 Answers

I had very similar but even weirder problem where I was updating one of my scope variables using the response from an HTTP call, surprisingly, my view will not update unless I make another HTTP call and by another I mean any other random HTTP call.

Using the $apply worked for me,

$scope.$apply(function () {
     $scope.resultData.totalResults = response.data.total;
});
like image 115
Saumil Avatar answered Sep 27 '22 20:09

Saumil