美文网首页我爱编程
[分析] angular directive中 $viewVal

[分析] angular directive中 $viewVal

作者: Top_Chenxi | 来源:发表于2017-04-23 17:02 被阅读194次

[分析] angular directive中 $viewValue和DOM节点的value 差别

工作上经常遇到dom赋值后发现model没变,或者model赋值后dom没变
所以写个dome来记录下原因

html


<div class="p20">
    <p>input的model值: {{testValue}}</p>
    <p>实际input展现出来的值:
        <input ngc-model-test type="text" ng-model="testValue" class="input-cfec">
    </p>
</div>

js


    var ngUM = angular.module('ngc.umeditor', []);

    ngUM.directive('ngcModelTest', function() {
        return {
            restrict: 'A',
            require: '?ngModel',
            scope: {
                ngModel: '=',
            },
            link: function(scope, element, attr, ngModel) {

                // 获取model值 ngModel.$viewValue
                // 设置model值 ngModel.$setViewValue('')

                // 获取dom值 element.val()
                // 设置dom值 element.val('')

                element.on('click', function(event) {
                    // action1();
                    // action2();
                    // action3();
                    // action4();
                    // action5();
                });

                function action1() {
                    element.val('1');
                    // 结果 
                    // input的model值: test
                    // 实际input展现出来的值: 1
                    // 结论
                    // 不仅model值没有变化,ngModel的$viewValue和$modelValue同样也没有变化
                    // input的value值不一定等于$viewValue
                }

                function action2() {
                    ngModel.$setViewValue('2');
                    // 结果 
                    // input的model值: 2
                    // 实际input展现出来的值: test
                    // 结论
                    // 执行完$setViewValue之后,无论是viewValue和modelValue都是已经同步了,
                    // 但是input里面的值却依然是test
                    // $viewValue和DOM节点的value是不同的
                }

                function action3() {
                    element.val('3');
                    ngModel.$render();
                    // 结果 
                    // input的model值: test
                    // 实际input展现出来的值: test
                    // 结论
                    // 通过dom赋值了3 ,但没有改变model 所以model的值还是 test
                    // 但是 $render 又把 model 重新赋值搭配 dom 里面
                    // 所以input里面的值却依然是test
                }

                function action4() {
                    ngModel.$setViewValue('4');
                    ngModel.$render();
                    // 结果 
                    // input的model值: 4
                    // 实际input展现出来的值: 4
                    // 结论
                    // 通过model赋值了 4  ,然后把model的值通过 $render() 赋值到dom里面
                    // 所以input里面的值为 4
                }

                // 看看 $render的源码
                /*
                    ngModel.$render = function() {
                        var value = ngModel.$isEmpty(ngModel.$viewValue) ? '' : ngModel.$viewValue;
                        element.val(value);
                    };
                */
                // 本质就是把 model 的值同步到 dom里面

                // 最后 正确的同步赋值如下
                // dom => model
                function setModelValue() {
                    var val = element.val();
                    ngModel.$setViewValue(val);
                }

                function action5() {
                    element.val('5');
                    scope.$apply(setModelValue);
                    // 把dom 的值同步到 model 里面
                }

                // model => dom

                // 一般都是重写 $render,比如以前编辑器里面的同步
                /*
                    ngModel.$render = function() {
                        _initContent = (ctrl.$isEmpty(ctrl.$viewValue) ? "" : ctrl.$viewValue);
                        // 拿到model进行dom赋值操作
                        if (_initContent && editor && _editorReady) {
                            editor.setContent(_initContent);
                        }
                    };
                */

            }
        }
    })
    

相关文章

  • [分析] angular directive中 $viewVal

    [分析] angular directive中 $viewValue和DOM节点的value 差别 工作上经常遇到...

  • 知识点

    1、Angular2+的版本中中没有scope,没有controller、directive,只有componen...

  • angular函数

    angular.js指令(directive)中的controller,compile,link函数有什么不同? ...

  • angular2 JIT and AOT

    为什么需要编译 Angular应用中包含的组件、HTML模板(比如:@Directive、@Component、@...

  • angularjs 之 directive

    这片文章为directive的基本的用法。 1.directive的定义 angular.module("app"...

  • Vue 3自定义指令开发

    什么是指令(directive) 在Angular和Vue中都有Directive的概念,我们通常讲Directi...

  • angular directive

    directive 是 angular 中最重要的一部分,也是最难的部分。Angular 内部封装了一些指令,例如...

  • 【angular】@Directive

    在 Angular中有三种类型的指令directive: 组件@Component — 拥有模板的指令,我们在另外...

  • angular 6 from rookie to master

    1. Creating a Custom Directive Angular provides a good ra...

  • 指令Angular 4 - Directives

    Angular中的指令是一个js类,它被声明为@directive。Angular中有3种指令表现形式。 组件的指...

网友评论

    本文标题:[分析] angular directive中 $viewVal

    本文链接:https://www.haomeiwen.com/subject/tysyzttx.html