angular4应用中输入的最小值和最大值的方法

 更新时间:2019年05月17日 09:07:44   投稿:zx  
这篇文章主要介绍了angular4应用中输入的最小值和最大值的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Angular4输入属性

输入属性通常用于父组件向子组件传递信息

举个栗子:我们在父组件向子组件传递股票代码,这里的子组件我们叫它app-order

首先在app.order.component.ts中声明需要由父组件传递进来的值

order.component.ts

...

@Input()

stockCode: string

@Input()

amount: string

...

order.component.html

<p>这里是子组件</p>

<p>股票代码为{{stockCode}}</p>

<p>股票总数为{{amount}}</p>

然后我们需要在父组件(app.component)中向子组件传值

app.component.ts

...

stock: string

...

app.component.html

<input type="text" placeholder="请输入股票代码" [(ngModel)]="stock">

<app-order [stockCode]="stock" [amount]="100"></app-order>

这里我们使用了Angular的双向数据绑定,将用户输入的值和控制器中的stock进行绑定。然后传递给子组件,子组件接收后在页面显示。

Angular4输出属性

当子组件需要向父组件传递信息时需要用到输出属性。

举个栗子:当我们从股票交易所获得股票的实时价格时,希望外部也可以得到这个信息。为了方便,这里的实时股票价格我们通过一个随机数来模拟。这里的子组件我们叫它app.price.quote

使用EventEmitter从子组件向外发射事件

price.quote.ts

export class PriceQuoteComponent implements OnInit{

 stockCode: string = 'IBM';

 price: number;

 //使用EventEmitter发射事件

 //泛型是指往外发射的事件是什么类型

 //priceChange为事件名称

 @Output()

 priceChange:EventEmitter<PriceQuote> = new EventEmitter();

 constructor(){

 setInterval(() => {

  let priceQuote = new PriceQuote(this.stockCode, 100*Math.random());

  this.price = priceQuote.lastPrice;

  //发射事件

  this.priceChange.emit(priceQuote);

 })

 }

 ngInit(){

 }

}

//股票信息类

//stockCode为股票代码,lastPrice为股票价格

export class PriceQuote{

 constructor(public stockCode:string,

  public lastPrice:number

 )

}

price.quote.html

<p>

 这里是报价组件

</p>

<p>

 股票代码是{{stockCode}}

</p>

<p>

 股票价格是{{price | number:'2.2-2'}}

</p>

接着我们在父组件中接收事件

app.component.html

<app-price-quote (priceChange)="priceQuoteHandler($event)"></app-price-quote>

<p>

 这是在报价组件外, 股票代码是{{priceQuote.stokcCode}},

 股票价格是{{priceQuote.lastPrice | number:'2.2-2'}}

</p>

事件绑定和原生的事件绑定是一样的,都是将事件名称放在()中。

app.component.ts

export class AppComponent{

 priceQuote:PriceQuote = new PriceQuote('', 0);

 priceQuoteHandler(event:PriceQuote){

 this.priceQuote = event;

 }

}

这里的event类型就是子组件传递事件的类型。

angular4应用中输入的最小值和最大值的方法

我有一个带有表单的angular4应用程序.在这个我输入一个百分比输入.所以,我想用0到100之间的值来阻止输入.

我试图添加min =“0”和max =“100”,但我仍然可以输入一个高于100或小于0的数字.

模板

<md-input-container>
 <input type="number" 
  maxlength="3" 
  min="0" 
  max="100" 
  required 
  mdInput 
  placeholder="Charge" 
  [(ngModel)]="rateInput" 
  name="rateInput">
 <md-error>Required field</md-error>
</md-input-container>

你知道我怎么做吗?

解决方法

我成功地使用了表单控件.

这是我的HTML代码:

<md-input-container>
    <input type="number" min="0" max="100" required mdInput placeholder="Charge" [(ngModel)]="rateInput" name="rateInput" [formControl]="rateControl">
    <md-error>Please enter a value between 0 and 100</md-error>
  </md-input-container>

在我的打字稿代码中,我有:

this.rateControl = new FormControl("",[Validators.max(100),Validators.min(0)])

因此,如果我们输入的值大于100或小于0,则材料设计输入变为红色且该字段未验证.所以之后,如果值不好,我点击保存按钮时就不保存.

总结

以上是脚本之家为你收集整理的angular4应用中输入的最小值和最大值全部内容,希望文章能够帮你解决angular4应用中输入的最小值和最大值所遇到的程序开发问题。

相关文章

  • 浅谈angular.copy() 深拷贝

    浅谈angular.copy() 深拷贝

    本篇文章主要介绍了浅谈angular.copy() 深拷贝,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Angular中ng-options下拉数据默认值的设定方法

    Angular中ng-options下拉数据默认值的设定方法

    本篇文章主要介绍了Angular中ng-options下拉数据默认值的设定方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Angular.js中$resource高大上的数据交互详解

    Angular.js中$resource高大上的数据交互详解

    这篇文章主要给大家介绍了关于Angular.js中$resource高大上的数据交互的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用angular.js具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。
    2017-07-07
  • Angular项目过大时的合理拆分angular split

    Angular项目过大时的合理拆分angular split

    这篇文章主要为大家介绍了Angular项目过大时的合理拆分angular split示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • AngularJS ng-repeat数组有重复值的解决方法

    AngularJS ng-repeat数组有重复值的解决方法

    不知道大家是否遇到过这个问题,在当Angular.JS ng-repeat数组中有重复项时,系统就会抛出异常,这是该怎么做?本文通过示例代码介绍了详细的解决方法,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-10-10
  • Angular学习教程之RouterLink花式跳转

    Angular学习教程之RouterLink花式跳转

    这篇文章主要给大家介绍了关于Angular学习教程之RouterLink花式跳转的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2018-05-05
  • AngularJS 最常用的功能汇总

    AngularJS 最常用的功能汇总

    angularjs功能在项目开发中经常会用到,本文给大家总结了八种angularjs最常用的功能,感兴趣的朋友一起学习吧
    2016-02-02
  • AngularJS实现全选反选功能

    AngularJS实现全选反选功能

    这篇文章主要介绍了AngularJS实现全选反选功能,这里用到AngularJS四大特性之二----双向数据绑定,对angularjs实现全选反选相关知识感兴趣的朋友一起学习吧
    2015-12-12
  • AngularJS基础知识

    AngularJS基础知识

    这篇文章主要介绍了AngularJS基础知识,包括AngularJS定义和特点以及构建AngularJS应用的方法,推荐给大家。
    2014-12-12
  • Angular重构数组字段的解决方法示例

    Angular重构数组字段的解决方法示例

    这篇文章主要为大家介绍了Angular重构数组字段的解决方法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09

最新评论