最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add new object to array angular - Stack Overflow

programmeradmin0浏览0评论

I'm trying to put a JSON object into an array after an API call.

First I made my API call and then I try to add every user into in a formatted JSON object.

connectionProvider.ts

import { UserModelProvider } from './../user-model/user-model';
import { MSAdal, AuthenticationContext, AuthenticationResult } from '@ionic-native/ms-adal';

export class MsConnectionProvider {
  userInfo : UserModelProvider;
  users: UserModelProvider[];

 constructor(...) {}

getUserInfo(){
    let header = new Headers({
      Authorization: this.accessToken;
    });
    let options = new RequestOptions({headers: header});

    this.usersSubscription = this.http.get(".0/groups/**ID**/members", options).map(res => res.json()['value']);
    this.usersSubscription.subscribe(res => {
      for (let user of res){
        this.addUserInfo(user.displayName, user.jobTitle, "something", user.mail);
      }
    });
  }

 addUserInfo(name, job, departement, mail){
    this.userInfo = new UserModelProvider;

    this.userInfo.name = name;
    this.userInfo.job = job;
    this.userInfo.departement = departement;
    this.userInfo.mail = mail;

    this.users.push(this.userInfo);
  }
}

userModelProvider.ts

export class UserModelProvider {
  name: string;
  job: string;
  departement: string;
  mail: string;
  photo: any;
}

The problem is when I try to push "this.userInfo = new UserModelProvider" into this.users array the function block and nothing happens.

I certainly don't understand the class, can you help me?

Thank you.

I'm trying to put a JSON object into an array after an API call.

First I made my API call and then I try to add every user into in a formatted JSON object.

connectionProvider.ts

import { UserModelProvider } from './../user-model/user-model';
import { MSAdal, AuthenticationContext, AuthenticationResult } from '@ionic-native/ms-adal';

export class MsConnectionProvider {
  userInfo : UserModelProvider;
  users: UserModelProvider[];

 constructor(...) {}

getUserInfo(){
    let header = new Headers({
      Authorization: this.accessToken;
    });
    let options = new RequestOptions({headers: header});

    this.usersSubscription = this.http.get("https://graph.microsoft./v1.0/groups/**ID**/members", options).map(res => res.json()['value']);
    this.usersSubscription.subscribe(res => {
      for (let user of res){
        this.addUserInfo(user.displayName, user.jobTitle, "something", user.mail);
      }
    });
  }

 addUserInfo(name, job, departement, mail){
    this.userInfo = new UserModelProvider;

    this.userInfo.name = name;
    this.userInfo.job = job;
    this.userInfo.departement = departement;
    this.userInfo.mail = mail;

    this.users.push(this.userInfo);
  }
}

userModelProvider.ts

export class UserModelProvider {
  name: string;
  job: string;
  departement: string;
  mail: string;
  photo: any;
}

The problem is when I try to push "this.userInfo = new UserModelProvider" into this.users array the function block and nothing happens.

I certainly don't understand the class, can you help me?

Thank you.

Share Improve this question edited Aug 23, 2018 at 9:29 danday74 57.4k55 gold badges269 silver badges333 bronze badges asked Aug 23, 2018 at 9:16 user7430961user7430961 351 silver badge6 bronze badges 1
  • 1 use new UserModelProvider(); – Krishna Rathore Commented Aug 23, 2018 at 9:18
Add a ment  | 

2 Answers 2

Reset to default 4

You can't push to an array that has not been initialised.

you need to change:

users: UserModelProvider[]

to:

users: UserModelProvider[] = [];

Also (may or may not help):

Nothing is probably happening because push mutates the array and as such angular change detection may not kick in.

Instead of using push, create a new array with:

this.users = [...this.users, this.userInfo]

or in ES5:

this.users = this.users.concat([this.userInfo])

Create an instance of the class before assigning the values,

this.userInfo = new UserModelProvider();
发布评论

评论列表(0)

  1. 暂无评论