调整文本区域大小时,Angular JS调整元素的大小

用户名

我正在建立一个指令,用于编写和预览类似于Github注释(对话)的注释。我的指令有两个HTML元素,一个textarea和一个div(使用角标记)。我想做的是在调整textarea的大小时调整div的大小。我已经扫描了该网站并在Google上进行了很多搜索,但没有一个纯粹的角度解决方案令人满意。

下面是我的指令简化:

    .directive('obibaCommentEditor', ['$log', '$timeout',
  function($log, $timeout) {
    return {
      restrict: 'E',
      replace: true,
      scope: {
        comment: '='
      },
      template: '<div><tab><textarea id="message" ng-model="comment.message"></textarea></tab><tab><div id="preview-message">{{comment.message}}</div></tab></div>',
      link: function(scope, elem, attrs) {
        function findChild(el, targetId) {
          var children = el.children;
          for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.id && child.id === targetId) {
              return child;
            }
            return findChild(child, targetId);
          }
        }

        $timeout(function() {
          scope.message = findChild(elem[0], 'message');
          $log.debug(scope.message.offsetWidth);
          resize();
        });

        //get preview element
        var previewElement = angular.element(document.querySelector('#preview-message'));

        function resize() {
          var dim = {
            width: scope.message.offsetWidth + "px",
            height: scope.message.offsetHeight + "px"
          };
          $log.debug(dim);
          previewElement.css(dim)
        }

        //Add the "mousemove" event to check, perhaps you can change the event "mouseup"
        elem.on("mouseup", function() {
          resize();
        });
      }
    };
  }
])

及其模板:

<form class="obiba-comment-form" name="form" role="form" ng-submit="send()">
  <tabset>
    <ul class="nav pull-right">
      <li>
        <a href="//guides.github.com/features/mastering-markdown/" target="_blank">{{'comment.markdown-doc-link' | translate}}</a>
      </li>
    </ul>
    <tab heading="{{'comment.write' | translate}}">
      <textarea id="obiba-comment-form-message" ng-model="comment.message" class="form-control obiba-comment-form-message"></textarea>
    </tab>
    <tab heading="{{'comment.preview' | translate}}">
      <div class="obiba-comment-marked" marked="comment.message">
      </div>
    </tab>
  </tabset>
  <button ng-if="onCancel" ng-click="cancel" type="submit" class="btn btn-default obiba-comment-form-submit">
    <span>{{'cancel' | translate}}</span>
  </button>

  <button ng-disabled="!comment.message" type="submit" class="btn btn-primary obiba-comment-form-submit">
    <span>{{'comment.send' | translate}}</span>
  </button>
</form> 

谢谢。

编辑:

我以为自己找到了解决方案,但就我而言,无法设置标记元素的高度。在简单的测试中,我曾使用ng-mouseup将标记的高度设置为消息的高度。在我的指令中,同一代码永远不会返回正确的高度(未定义)或offsetHeight(0)。因此,普遍的问题是,到底我们如何在纯AngularJS中修改DOM元素?我阅读了大多数文档,没有计时器等找不到改变元素高度的下降方式。

好吧,如果您查看了Github评论部分,并且知道如何在Anhgular中执行相同的操作,请分享该方法。

编辑2:

这是我发现的糟糕解决方案:

.directive('obibaCommentEditor', ['$log', '$timeout',
  function($log, $timeout) {
    return {
      restrict: 'E',
      replace: true,
      scope: {
        comment: '='
      },
      template: '<div><tab><textarea id="message" ng-model="comment.message"></textarea></tab><tab><div id="preview-message">{{comment.message}}</div></tab></div>',
      link: function(scope, elem, attrs) {
        function findChild(el, targetId) {
          var children = el.children;
          for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.id && child.id === targetId) {
              return child;
            }
            return findChild(child, targetId);
          }
        }

        $timeout(function() {
          scope.message = findChild(elem[0], 'message');
          $log.debug(scope.message.offsetWidth);
        });

        //get preview element
        var previewElement = angular.element(document.querySelector('#preview-message'));

        function resize() {
          var dim = {
            width: scope.message.offsetWidth + "px",
            height: scope.message.offsetHeight + "px"
          };
          $log.debug(dim);
          previewElement.css(dim)
        }

        //Add the "mousemove" event to check, perhaps you can change the event "mouseup"
        elem.on("mouseup", function() {
          resize();
        });

        resize();
      }
    };
  }
])

请注意,该代码基于https://stackoverflow.com/users/1074519/wzvang提供的代码

干杯。

沃尔特·查皮利肯(Walter Chapilliquen)-wZVanG

当您单击“预览”选项卡时,Github中预览元素的大小会更改,在这里我创建了一个类似的指令:

angular.module("MyApp",[])

.directive("resizeTextarea", [function(){
    return function(scope, elem, attrs){

        //save current size of textarea
        var current = {width: elem[0].offsetWidth, height: elem[0].offsetHeight}

        //get preview element
        var previewElement = angular.element(document.querySelector(attrs.resizeTextarea));

        function resize(){
            previewElement.css({width: current.width + "px", height: current.height + "px"})
        }

        //Add the "mousemove" event to check, perhaps you can change the event "mouseup"
        elem.on("mousemove", function(){

            //detect if the textarea has not the initial size
            if(this.offsetWidth != current.width || this.offsetHeight != current.height){
                current = {width: this.offsetWidth, height: this.offsetHeight};
                resize()
            }

        });

        resize()
    }
}]);
<tab>
    <textarea resize-textarea="#preview-message" ng-model="comment.message" class="obiba-comment-form-message"></textarea>
</tab>
<tab>
    <div id="preview-message">{{comment.message}}</div>
</tab>

演示版

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

调整窗口大小时,按类名称触发调整文本区域的大小

来自分类Dev

调整文本区域大小时,所有元素都将向下移动:如何修复

来自分类Dev

调整jHtml文本区域的大小

来自分类Dev

jQuery自动调整文本区域的大小

来自分类Dev

自动调整文本区域大小

来自分类Dev

根据内容调整文本区域的大小

来自分类Dev

根据内容自动调整文本区域的大小

来自分类Dev

jQuery和自动调整文本区域大小的问题

来自分类Dev

在引导程序中自动调整文本区域的大小

来自分类Dev

自动调整页面上多个文本区域的大小

来自分类Dev

如何创建将始终调整大小的文本区域?

来自分类Dev

有关调整文本区域大小的指针

来自分类Dev

调整文本区域的大小以垂直适合页面

来自分类Dev

在引导程序中自动调整文本区域的大小

来自分类Dev

引导模式显示先前调整大小的文本区域

来自分类Dev

从元素输入中填充时自动调整文本区域的大小-Javascript

来自分类Dev

调整大小时混乱的元素

来自分类Dev

无法将文本区域调整为适合Windows大小的大小

来自分类Dev

<div>元素:调整大小时如何使div文本保持原样

来自分类Dev

可调整大小的文本区域,可在键入时不断调整大小,而不必等待内容溢出

来自分类Dev

调整旋转元素的大小

来自分类Dev

调整svg元素的大小

来自分类Dev

调整画布元素的大小

来自分类Dev

调整<canvas>元素的大小

来自分类Dev

调整窗口元素大小时移动

来自分类Dev

调整大小时使响应容器与元素对齐

来自分类Dev

调整窗口大小时防止元素换行

来自分类Dev

调整屏幕大小时隐藏元素

来自分类Dev

在窗口调整大小时隐藏元素

Related 相关文章

热门标签

归档