简体   繁体   English

角度ngModel样式

[英]angular ngModel style

Is it possible to style the value in the attribute ngModel of an input tag? 是否可以在输入标签的ngModel属性中设置值的样式?

Example: 例:

<input class="input" type="text" [(ngModel)] = "myService.text">

Let's say the value of text is '28 packages', can I put 28 in bold? 假设text的值为“ 28个包装”,我可以将28个粗体显示吗?

So if i understand correctly you want to have it bold whenever the value is 28 ? 因此,如果我理解正确,那么无论何时值28,您都希望将其设为粗体?

yes its possible you can use a ng-class with a ternary expression like this 是的,您可以使用具有此类三元表达式的ng-class

 .bold{ font-weight:600; } 
 <input type="text" ng-class="myService.text == '28 ? 'bold' : '''" class="input" ng-model="myService.text" /> 

This is not angular-related rather a CSS related question. 这与角度无关,而与CSS有关。

You cannot style only a part of an input in HTML/CSS so you won't be able to do it in angular. 您不能在HTML / CSS中仅对输入的一部分进行样式设置,因此将无法按角度进行输入。

Instead, you can use an input that is hidden behind a div. 相反,您可以使用隐藏在div后面的输入。 The idea is that when the user clicks the div, you actually focus the input. 这个想法是,当用户单击div时,您实际上是在集中输入。 When the user types text, you capture the content of the input and fill the div with it, eventually adding <span class"highlight"> around the number of packages. 当用户键入文本时,将捕获输入内容并用div填充div,最终在包数附近添加<span class"highlight">

I prepared you a stackblitz in pure CSS/JS. 我为您准备了纯CSS / JS的堆叠闪电战 You can adapt it in angular if you want. 您可以根据需要调整角度。

Relevant pieces of code : 相关代码段:

HTML : HTML

<span id="hiddenSpan">This is the hidden div. Click it and start typing</span>

<div>
  <label for="in">The real input</label>
  <input id="in" type="text">
</div>

JS : JS

const input = document.getElementById('in')
const hiddenSpan = document.getElementById('hiddenSpan')

function onInputChanged() {
  let text = input.value

  const regex = new RegExp('(\\d+) packages')
  let result = regex.exec(text)

  if(result) {
    hiddenSpan.innerHTML = '<span class="highlight">'+result[1]+'</span> packages'
  } else {
    hiddenSpan.innerHTML = text
  }
}

// Capture keystrokes.
input.addEventListener('keyup', onInputChanged)

// Focus the input when the user clicks the pink div.
hiddenSpan.addEventListener('click', function() {
  input.focus()
})

CSS : CSS

#hiddenSpan {
  background-color: pink;
}

.highlight {
  font-weight: bold;
  background-color: greenyellow;
}

Note : the downside is that the blinking caret is not visible anymore. 注意 :缺点是闪烁的插入符号不再可见。 You can take a look at this resource if you want to simulate one. 如果要模拟一个资源,可以看一下。

It is not possible to style certain parts of a text <input> field in bold. 不能用粗体显示文本<input>字段的某些部分的样式。 However, you can use a contenteditable div instead of a text <input> field. 但是,可以使用contenteditable div代替文本<input>字段。 Inside the contenteditable div you can have other HTML tags like <strong> to style certain parts of the text however you like. contenteditable div内,您可以使用其他HTML标记(例如<strong>来随意设置文本的某些部分的样式。

I created an Angular directive called contenteditableModel (check out the StackBlitz demo here ) and you can use it to perform 2-way binding on a contenteditable element like this: 我创建了一个名为contenteditableModel的Angular指令( 在此处查看StackBlitz演示 ),您可以使用它对contenteditable元素执行双向绑定,如下所示:

<div class="input" contenteditable [(contenteditableModel)]="myService.text"></div>

The directive uses regular expressions to automatically check for numbers in the inputted text, and surrounds them in a <strong> tag to make them bold. 该指令使用正则表达式自动检查输入文本中的数字 ,并将其括在<strong>标记中以使其加粗。 For example, if you input "28 packages", the innerHTML of the div will be formatted like this (to make "28" bolded): 例如,如果您输入“ 28个包”,则divinnerHTML会像这样格式化(使“ 28”加粗):

<strong>28</strong> packages

This is the code used in the directive to perform the formatting: 这是指令中用于执行格式化的代码:

var inputElement = this.elementRef.nativeElement;
inputElement.innerHTML = inputElement.textContent.replace(/(\d+)/g, "<strong>$1</strong>");
this.change.emit(inputElement.textContent);

You can change the <strong> tag to something else (eg <span style="text-decoration: underline"> if you want the text to be underlined instead of bolded). 您可以将<strong>标记更改为其他标记(例如,如果希望文本加下划线而不是加粗,则为<span style="text-decoration: underline"> )。

When performing the formatting, there is an issue where the user's text cursor position will be unexpectedly reset back to the beginning of the contenteditable div. 执行格式化时,存在一个问题,即用户的文本光标位置将被意外重置回contenteditable div的开头。 To fix this, I used 2 functions ( getOriginalCaretPosition and restoreCaretPosition ) to store the user's original cursor position and then restore the position back after the text formatting is performed. 为了解决这个问题,我使用了2个函数( getOriginalCaretPositionrestoreCaretPosition )来存储用户的原始光标位置,然后在执行文本格式设置后恢复该位置。 These 2 functions are kind of complex and they're not entirely relevant to the OP's question so I will not go into much detail about them here. 这两个函数有点复杂,它们与OP的问题并不完全相关,因此在这里我不会对其进行详细介绍。 You can PM me if you want to learn more about them. 如果您想了解更多关于他们的信息,可以联系我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM