大佬教程收集整理的这篇文章主要介绍了数组 – Angular2教程:本节中的ID变量如何自动递增?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道Arrays.push()返回数组的长度,是自动插入Hero类中的id变量的长度吗?
在hero.services.ts中有一段代码来创建一个英雄:
create(name: String): Promise<Hero> { return this.http .post(this.heroesUrl,JSON.Stringify({name: namE}),{headers: this.headers}) .toPromise() .then(res => res.json().data) .catch(this.handleError); }
add(name: String): void { name = name.trim(); if (!Name) { return; } this.heroservice.create(Name) .then(hero => { this.heroes.push(hero); this.SELEctedHero = null; }); }
https://github.com/angular/in-memory-web-api/blob/master/in-memory-backend.service.js
在该处理程序内部,id通过调用genId函数生成,该函数的实现位于第257行:
InMemoryBACkendservice.prototype.genId = function (collection) { // assumes numeric ids var maxId = 0; collection.reduce(function (prev,item) { maxId = Math.max(maxId,typeof item.id === 'number' ? item.id : maxId); },null); return maxId + 1; };
以上是大佬教程为你收集整理的数组 – Angular2教程:本节中的ID变量如何自动递增?全部内容,希望文章能够帮你解决数组 – Angular2教程:本节中的ID变量如何自动递增?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。