加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – ngModel $modelValue和ngModel之间有什么区别$view

发布时间:2020-12-17 09:08:59 所属栏目:安全 来源:网络整理
导读:我有以下ckEditor指令。下面是从示例中看到的如何在编辑器中设置数据的两个变体: app.directive('ckEditor',[function () { return { require: '?ngModel',link: function ($scope,elm,attr,ngModel) { var ck = null; var config = attr.editorSize; if (c
我有以下ckEditor指令。下面是从示例中看到的如何在编辑器中设置数据的两个变体:
app.directive('ckEditor',[function () {
    return {
        require: '?ngModel',link: function ($scope,elm,attr,ngModel) {

            var ck = null;
            var config = attr.editorSize;
            if (config == 'wide') {
                ck = CKEDITOR.replace(elm[0],{ customConfig: 'config-wide.js' });
            } else {
                ck = CKEDITOR.replace(elm[0],{ customConfig: 'config-narrow.js' });
            }


            function updateModel() {
                $scope.$apply(function () {
                    ngModel.$setViewValue(ck.getData());
                });
            }

            $scope.$on('modalObjectSet',function (e,modalData) {
                // force a call to render
                ngModel.$render();
            });

            ck.on('change',updateModel);
            ck.on('mode',updateModel);
            ck.on('key',updateModel);
            ck.on('dataReady',updateModel);

            ck.on('instanceReady',function () {
                ngModel.$render();
            });

            ck.on('insertElement',function () {
                setTimeout(function () {
                    $scope.$apply(function () {
                        ngModel.$setViewValue(ck.getData());
                    });
                },1000);
            });

            ngModel.$render = function (value) {
                ck.setData(ngModel.$modelValue);
            };

            ngModel.$render = function (value) {
                ck.setData(ngModel.$viewValue);
            };
        }
    };
}])

有人可以告诉我有什么区别:

ck.setData(ngModel.$modelValue);
ck.setData(ngModel.$viewValue);

和我应该使用。我看了角度文档,它说:

$viewValue

Actual string value in the view.

$modelValue

The value in the model,that the control is bound to.

我不知道作者的意思,当他在文档中写这个:-(

你正在看正确的文档,但它可能只是你有点困惑。 $ modelValue和$ viewValue有一个明显的区别。正是这样:

如上所述:

$viewValue: Actual string (or Object) value in the view.
$modelValue: The value in the model,that the control is bound to.

我要假设你的ngModel是指元件…?所以你的有一个字符串值显示给用户,对不对?但实际的模型可能是该字符串的某个其他版本。例如,输入可能显示字符串’200’,但是 (例如)实际上将包含200的模型值作为整数。因此,在中“查看”的字符串表示是ngModel。$ viewValue,数字表示将是ngModel。$ modelValue。

另一个示例是其中$ viewValue将类似于2000年1月1日,$ modelValue将是表示该日期字符串的实际的JavaScript Date对象。那有意义吗?

我希望回答你的问题。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读