为什么这个 Angular JS 过滤器会去除空格?

编程入门 行业动态 更新时间:2024-10-27 21:10:57
本文介绍了为什么这个 Angular JS 过滤器会去除空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在制作一个小应用程序来学习 Angular,并制作了一个用于文本处理的自定义过滤器.一切正常,除了在我的输出文本有多个空格的情况下,这些会自动减少为一个空格,这是我不想要的.问题不在我定义的函数中,因为我正在记录没有问题的输出.

HTML:

"

<textarea name="textarea" rows="10" cols="50" ng-model="encodeText" placeholder="输入或粘贴要编码的消息."></textarea><p>过滤后的输入:{{ encodeText |编码}}</p>

JS:

(function(){var app = angular.module('myApp', []);app.filter("encode", function() {返回函数(输入){如果(输入){//文本处理控制台日志(输出);返回输出;}};});

这是一个莫尔斯电码练习.所以控制台日志输出是:

.- -... -.-.-.....-.

有两个空格.在页面上我看到:

过滤输入:.- -... -.-.-.....-.

我从谷歌上看到的唯一建议是在 textarea 上使用 ng-trim="false",这没有效果.看起来修剪是在过滤器本身内发生的.这是什么原因造成的,我该如何禁用它?代码仓库

解决方案

p格式化您的输出.试试:

{ encodeText |编码 }

I'm making a tiny app to learn Angular, and have made a custom filter for text processing. Everything is working fine, except that in cases when my output text has multiple spaces, these get automatically reduced to a single space, which I don't want. The issue is not in the function I've defined, because I'm logging the output, which does not have the problem.

HTML:

<textarea name="textarea" rows="10" cols="50" ng-model="encodeText" placeholder="Type or paste in a message to encode." ></textarea>
<p>Filtered input: {{ encodeText | encode }}</p>

JS:

(function(){
  var app = angular.module('myApp', []);
  app.filter("encode", function() {
    return function(input) {
      if (input) {
        //text processing
        console.log(output);
        return output;
      }
    };
 });

It is a Morse Code exercise. So console log output is:

.- -... -.-.   -.. . ..-. 

With two spaces. On the page I see:

Filtered input: .- -... -.-. -.. . ..-.

The only suggestion I'm seeing from Googling is using ng-trim="false" on the textarea, which has no effect. It seems the trimming is happening within the filter itself. What is causing this and how do I disable it? Code repo

解决方案

The pformat your output. Try:

<pre>{ encodeText | encode }</pre>

这篇关于为什么这个 Angular JS 过滤器会去除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 10:24:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1000405.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   空格   Angular   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!