Angular $ scope变量未更新 [英] Angular $scope variable not updating

查看:91
本文介绍了Angular $ scope变量未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的角度中,我定义了一个范围变量 $ scope.letter_content 。加载视图时,我从数据库加载字符串并将其设置为 $ scope.letter_content 。然后,我填写我正在使用的texteditor(Froala)。

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.

以下是该视图的代码:

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

所以基本上我将 letter_content 设为ng-发短信的模型。因此,当我对texteditor进行更改时,它会修改值 $ scope.letter_content

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.

我发现奇怪的是,当我修改短信的文本时,它会改变 {{letter_content}} 在div里面。但是,它不会在div之外更新 {{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.

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

为什么会发生这种奇怪的事情?

Why is this weird thing happening?

推荐答案

其实这有已在更多链接中讨论过。

Actually this has been discussed in more links.

不要使用原始类型变量。而不是在范围内使用对象。

Don't use primitive type variable. Instead of that use object in scope.

例如,
不要使用像 $ scope.primitiveVariale 而不是这个 $ scope.object = {primitiveVariale:null}

所以在视图中使用像 object.primitiveVariale 然后这将反映在所有视图中。请参阅以下链接。

So in view use like object.primitiveVariale then this will be reflect in all the view. Please see the following links.

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

这篇关于Angular $ scope变量未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆