【问题标题】:ngClass: Check if input has value with angularngClass:检查输入是否具有角度值
【发布时间】:2019-05-04 14:14:48
【问题描述】:

我有一个 angular component 用于 input 和浮动 label。我切换了一个boolean 变量,用于在input 包装器上设置class,以使labelanimationangular (blur)(focus) 上的input 元素上的浮动(focus)。现在这工作正常,当我单击 input 时,label 动画向上,当我离开 input 时动画向下动画。我唯一的问题是,当我在input 中输入一个值并离开它时,label 应该保持浮动在input 之上。目前,它会向下动画并与value 重叠。例如,我如何检测我的ngClass,如果isFocusedtrue label 有一个value,以保持class

这是我的代码。在stackoverflow sn-p 上,只支持angularjs,所以我无法在这里制作sn-p,抱歉!

HTML:

<div class="my-input" [ngClass]="isFocused ? 'state-my-input--active' : ''">
  <div class="my-input__wrapper">
    <label class="my-input__label">Label</label>
    <input type="text" class="my-input__input" (blur)="isFocused = false" (focus)="isFocused = true">
  </div>
</div>

SCSS:

.my-input {
  display: flex;
  flex-direction: column;
  margin-top: 50px;
}

.my-input__wrapper {
  display: flex;
  flex-direction: column;
  position: relative;
}

.my-input__label {
  bottom: 8px;
  color: blue;
  font-size: 14px;
  position: absolute;
  transition: all 0.3s ease-in-out;
}

.my-input__input {
  border: none;
  border-bottom: 2px solid darkgray;
  color: blue;
  transition: border-color 180ms linear;
}

.state-my-input--active {
  .my-input__wrapper {
    .my-input__label {
      transform: translateY(-15px);
      cursor: default;
    }

    .my-input__input {
      border-bottom-color: blue;
    }
  }
}

JS:

import { Component, OnInit } from "@angular/core";

@Component({
    selector: "my-input",
    templateUrl: "./my-input.component.html",
    styleUrls: ["./my-input.component.scss"]
})
export class MyInputComponent implements OnInit {
    isFocused: boolean = false;

    constructor() {}

    ngOnInit() {}
}

【问题讨论】:

  • 改用[class.state-my-input--active]="isFocused"
  • 如果label 有一个值或者Input 有一个值你的意思是?
  • @selemmn 如果输入有值
  • @MrBuggy 你已经有了答案..
  • @selemmn 是的,看到了。我找到了基于其中一个答案的解决方案!谢谢

标签: javascript html css angular input


【解决方案1】:

试试:

[ngClass]="{'state-my-input--active': isFocused}"

【讨论】:

  • 这不起作用,它只是另一种写焦点条件的方式。使用此解决方案,当我离开输入并与键入的值重叠时,标签会浮动回来。我的问题是,如何将浮动标签保持在输入上方,如果输入中有值,离开时也是如此。
【解决方案2】:

首先在您的输入中定义[(ngModel)]。然后尝试使用这种方法添加类。

<div class="my-input" [ngClass]="{'state-my-input--active': isFocused || someValue}">
  <div class="my-input__wrapper">
    <label class="my-input__label">Label</label>
    <input type="text" class="my-input__input" [(ngModel)]="someValue" (blur)="isFocused = false" (focus)="isFocused = true">
  </div>
</div>

或者您可以为浮动标签使用角度材质。查看示例请点击here

【讨论】:

  • 这个答案帮助我解决了我的问题!通过在输入中添加[(ngModel)] 并将[ngClass] 中的条件更改为:[ngClass]="{'state-my-input--active': isFocused || someValue}" 它按预期工作!标签在为空和焦点时在上方浮动,如果为空,则返回浮动,如果在输入中有值离开,则保持浮动!谢谢!
【解决方案3】:

要获得条件中的值,只需使用(当然你必须先定义和绑定值:

[ngClass]="{'state-my-input--active': isFocused || value != ''}"

[class.state-my-input--active]="isFocused || value != ''"

https://angular.io/api/common/NgClass

【讨论】:

  • 我尝试了第一个解决方案,将[ngClass]input 上的[(ngModel)] 结合使用。我还必须将条件从value != '' 更改为value != undefined,因为使用您的解决方案,标签始终位于输入上方,不知道为什么。目前,当我单击输入时,标签浮动在输入上方,我可以输入一个值并离开输入并保留。当我返回输入并删除值并离开输入时,标签仍保持在输入上方,它不会向后浮动。知道为什么吗?
  • 也许这次的值不是未定义而是空的。您是否尝试组合使用“value!= ''”和“value!= undefined”?可能您必须测试“未定义”,因为您的值未使用 null 或空字符串初始化。
  • 我尝试了这种组合,但没有奏效。检查标记为正确的答案,我的解决方案适用于 ngModel 和 focus
  • Checkout stackblitz.com/edit/angular-pwbmff 对我来说它工作正常。
猜你喜欢
  • 2020-02-03
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
相关资源
最近更新 更多