简体   繁体   English

Angular $ scope变量未更新

[英]Angular $scope variable not updating

In my angular, I define a scope variable $scope.letter_content . 在我的角度,我定义了一个范围变量$scope.letter_content When the view is loaded, I load string from my database and set it to $scope.letter_content . 加载视图时,我从我的数据库加载字符串并将其设置为$scope.letter_content Then, I populate on a texteditor(Froala) i'm using. 然后,我填写我正在使用的texteditor(Froala)。

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. 所以基本上我将letter_content设置为texteditor的ng-model。 So when I make changes on the texteditor, it modifies the value $scope.letter_content . 因此,当我对texteditor进行更改时,它会修改值$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. 我发现奇怪的一件事是,当我修改texteditor中的文本时,它会更改div中的{{letter_content}} However, it does not update {{letter_content}} outside the div. 但是,它不会更新div之外的{{letter_content}}

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 . 当我在texteditor中编辑文本时,我发送一个put请求来使用$scope.letter_content更新数据库中的值。 However, it ends up sending {{letter_content}} outside the div which ends up not updating the content. 但是,它最终会在div之外发送{{letter_content}} ,最终不会更新内容。

Why is this weird thing happening? 为什么会发生这种奇怪的事情?

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. 我有一个非常相似甚至更奇怪的问题,我使用HTTP调用的响应更新我的一个scope变量,令人惊讶的是,除非我进行另一个HTTP调用,否则我的视图不会更新,而另一个我的意思是任何其他随机HTTP调用。

Using the $apply worked for me, 使用$apply为我工作,

$scope.$apply(function () {
     $scope.resultData.totalResults = response.data.total;
});

Actually this has been discussed in more links. 实际上这已在更多链接中讨论过。

Don't use primitive type variable. 不要使用原始类型变量。 Instead of that use object in scope. 而不是在范围内使用对象。

For example, Don't use like $scope.primitiveVariale instead of this $scope.object={primitiveVariale:null} 例如,不要使用$scope.primitiveVariale而不是$scope.object={primitiveVariale:null}

So in view use like object.primitiveVariale then this will be reflect in all the view. 所以在视图中使用像object.primitiveVariale那样这将反映在所有视图中。 Please see the following links. 请参阅以下链接。

https://github.com/angular/angular.js/wiki/Understanding-Scopes https://github.com/angular/angular.js/wiki/Understanding-Scopes

The ng-if directive create a new scope, so the letter_content inside the div is in the ng-if scope, but the letter_content outside is in the controller scope. ng-if指令创建一个新范围,因此divletter_content位于ng-if范围内,但外部的letter_content位于控制器范围内。

Read the API doc to know which directive create new scope. 阅读API文档以了解哪个指令创建新范围。

To avoid this kind of problem consider using the controllerAs syntax 为避免此类问题,请考虑使用controllerAs语法

can you ever heard of angular's dot notation ? 你能听说过棱角dot notation吗?

solution

put letter_content in an object. letter_content放在一个对象中。 froalaDetails.letter_content for example. froalaDetails.letter_content为例。 then update that value and everything should work exactly like you want. 然后更新该值,一切都应该像你想要的那样工作。

explanation 说明

watch this very short video to get the full details: https://egghead.io/lessons/angularjs-the-dot 观看这段非常短的视频,了解详情: https//egghead.io/lessons/angularjs-the-dot

Do not use plain values like this, you need to put such values into an object, so try in your controller: 不要使用这样的普通值,您需要将这些值放入对象中,因此请在控制器中尝试:

$scope.obj = { letter_content: null};

and then in your view: 然后在你看来:

{{obj.letter_content}}

Try this - 试试这个 -

 $scope.object = {letter_content:null};

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM