加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

增加焦点输入的宽度为angular2

发布时间:2020-12-17 17:06:07 所属栏目:安全 来源:网络整理
导读:我是angular2的新手,我有一个问题,我的输入有一个属性autoGorw它是一个自定义指令,有两个事件(焦点)和(模糊),onFocus()我想增加输入大小和onBlur()我想要将大小减小到默认大小,事件一直在触发并在控制台中显示结果,但输入大小没有增加,我的控制台中没有任何
我是angular2的新手,我有一个问题,我的输入有一个属性autoGorw它是一个自定义指令,有两个事件(焦点)和(模糊),onFocus()我想增加输入大小和onBlur()我想要将大小减小到默认大小,事件一直在触发并在控制台中显示结果,但输入大小没有增加,我的控制台中没有任何错误,我不知道我缺少什么.

对不起,我在Plunker尝试了现场演示,让你很容易理解,但无法制作一个.

这是我的代码

自动grow.directive.ts

import {Directive,ElementRef,Renderer} from '@angular/core'

// ElementRef => Gives access to host element
// Renderer => Gives access to modify that element

@Directive({
  selector: '[autoGrow]',host:{
    '(focus)' : 'onFocus()','(blur)' : 'onBlur()'
  }
})
export class AutoGrowDirective{
constructor(private el : ElementRef,private renderer : Renderer){       
}    
onFocus(){
    console.log("Triggered !"); // its working upto this line.
    this.renderer.setElementStyle(this.el.nativeElement,'Width','500');
}
onBlur(){
    this.renderer.setElementStyle(this.el.nativeElement,'120');
 }
}

courses.component.ts

import {Component} from '@angular/core'
import {AutoGrowDirective} from './auto-grow.directive'

@Component({
  selector:"courses",template:`
  <h2>This is Courses component</h2>
  <p>{{ title }}</p>
  <input type="text" autoGrow />
  `,directives: [AutoGrowDirective] 
})

export class CoursesComponent{
  title = 'This is the title of Courses Page!';
}

app.component.ts

import { Component } from '@angular/core';
import {CoursesComponent} from './courses.component';

@Component({
  selector: 'my-app',template: '<h1>Hello Angular!</h1><courses></courses>',directives:[CoursesComponent]
})
export class AppComponent { }

HTML

在html里面的身体我有选择器

<my-app>Loading Please wait...</my-app>

解决方法

我想你只是在’500px’中缺少px而’width’应该是小写的.

我会这样做:

@Directive({
  selector: '[autoGrow]',})
export class AutoGrowDirective {
  @HostBinding('style.width.px')
  width:number = 120;

  @HostListener('focus')
  onFocus() {
    this.width=500;
  }

  @HostListener('blur')
  onBlur(){
    this.width = 120;
  }
}

Plunker example

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读