Angular 向模型中添加元素
我试图在模型中添加一些元素。我不知道在Angular中是如何工作的,所以我试着添加 模型 TS 返回 无法将类型为“number”的参数分配给类型为的参数 “个人资料”Angular 向模型中添加元素,angular,typescript,Angular,Typescript,我试图在模型中添加一些元素。我不知道在Angular中是如何工作的,所以我试着添加 模型 TS 返回 无法将类型为“number”的参数分配给类型为的参数 “个人资料” 有人知道如何使用模型在数组中添加元素吗?您需要推送Profile classshape的对象基或peofile类的瞬间 perfilesTodos: Profile[] = []; // It certainly helps to understand that push takes varargs arguments. So
有人知道如何使用模型在数组中添加元素吗?您需要推送Profile classshape的对象基或peofile类的瞬间
perfilesTodos: Profile[] = []; // It certainly helps to understand that push
takes varargs arguments. So below would (like you are doing in your code) would push many elements to a list:
list.push(1,2,3,4,5);
perfilesTodos:Profile[]=[];// 您需要推送Profile类shape的对象基或文件类的瞬间
perfilesTodos: Profile[] = []; // It certainly helps to understand that push
takes varargs arguments. So below would (like you are doing in your code) would push many elements to a list:
list.push(1,2,3,4,5);
perfilesTodos:Profile[]=[];// 这当然有助于理解接受varargs参数。因此,以下内容(就像您在代码中所做的那样)会将许多元素推送到一个列表中:
this.perfilesTodos.push({ id: index, nombre : "Arson"});
因此,您需要做的是将配置文件添加到配置文件列表中
const data: string[] = ['Arson', 'Administrador de entidad', 'Administrador de grupo', 'Gestor', 'Instalador']
现在,为了省略所有那些if-else
语句,可以创建一个简单的字符串数组
perfilesTodos: Profile[] = [];
response["body"].forEach((id: number) => {
this.perfilesTodos.push({id, nombre: data[ response["body"] - 1 ]});
});
抛开所有的“如果”和“如果”而简单地去做
当然,您还必须更改nombre
的类型,因为没有description
这样的东西-它是字符串
看起来仍然很奇怪的是,您首先在'response['body']上循环,因为它是一个数组,然后尝试对一个数字进行相等性检查…理解它接受varargs参数当然很有帮助。因此,以下内容(就像您在代码中所做的那样)会将许多元素推送到一个列表中:
this.perfilesTodos.push({ id: index, nombre : "Arson"});
因此,您需要做的是将配置文件添加到配置文件列表中
const data: string[] = ['Arson', 'Administrador de entidad', 'Administrador de grupo', 'Gestor', 'Instalador']
现在,为了省略所有那些if-else
语句,可以创建一个简单的字符串数组
perfilesTodos: Profile[] = [];
response["body"].forEach((id: number) => {
this.perfilesTodos.push({id, nombre: data[ response["body"] - 1 ]});
});
抛开所有的“如果”和“如果”而简单地去做
当然,您还必须更改nombre
的类型,因为没有description
这样的东西-它是字符串
看起来仍然很奇怪的是,您首先在“response['body']上循环,因为它是一个数组,然后尝试对一个数字进行相等检查…请指定您得到的响应。我们不知道什么是response.body
它应该是this.perfilesTodos.push({id:index;nombre:“Arson”})代码>使用这个.perfilesTodos.push({id:index;nombre:“Arson”});已修复错误,但console.log(this.perfilesTodos)返回null。响应返回[1,2,3,4,5]对push for的所有项目执行此操作,例如:this.perfilesTodos.push({id:index;nombre:“Instalador”});根据响应[“body”]的值,将填充数组,记录响应[“body”]并查看它是什么?在这种情况下,return无法读取未定义的属性“push”。响应[“body”].forEach((索引:number)=>{console.log(索引);this.perfilesTodos.push({id:index,description:“Arson”});});请同时指定您得到的响应。我们不知道什么是response.body
它应该是this.perfilesTodos.push({id:index;nombre:“Arson”})代码>使用这个.perfilesTodos.push({id:index;nombre:“Arson”});已修复错误,但console.log(this.perfilesTodos)返回null。响应返回[1,2,3,4,5]对push for的所有项目执行此操作,例如:this.perfilesTodos.push({id:index;nombre:“Instalador”});根据响应[“body”]的值,将填充数组,记录响应[“body”]并查看它是什么?在这种情况下,return无法读取未定义的属性“push”。响应[“body”].forEach((索引:number)=>{console.log(索引);this.perfilesTodos.push({id:index,description:“Arson”});});在控制台中返回“未定义”。也许如果..else不起作用?我尝试这样的简单代码,如果(response[“body”]==1){console.log(“entra”);}在foreach中,但不起作用pass@ElHombreSinNombre该错误与数组推送有关method@ElHombreSinNombre可能您需要在控制台中使用空数组检查my answerReturn'Unfinied'初始化perfilesTodos数组。也许如果..else不起作用?我尝试这样的简单代码,如果(response[“body”]==1){console.log(“entra”);}在foreach中,但不起作用pass@ElHombreSinNombre该错误与数组推送有关method@ElHombreSinNombre可能您需要使用空数组初始化perfilesTodos数组检查my answerresponse[“body”]。forEach((id:number)=>{this.perfilesTodos.push({id,description:“Arson”});});显然有效,但我需要创建if..else以设置具有不同id的名称。否,正如我解释的,在本例中,您不需要@elhombresinnombre这个.perfilesTodos.push({id,name:this.data[response][“body”]-1]});返回空名称,我使用this.perfilesTodos.push({id,description:this.data[id-1]});response[“body”].forEach((id:number)=>{this.perfilesTodos.push({id,description:“Arson”});});显然有效,但我需要创建if..else以设置具有不同id的名称。否,正如我所解释的,在本例中,您不需要@elhombresinnombre this.perfilesTodos.push({id,name:this.data[response[“body”]-1]});返回空名称,我使用这个.perfilesTodos.push修复它({id,description:this.data[id-1]});