Переменная Typescript сбрасывается после установки - PullRequest
0 голосов
/ 30 декабря 2018

У меня проблема, и я пока не смог найти ответ.Я создаю новый экземпляр класса ProfileData и устанавливаю переменную profileName с помощью setName ().Я могу видеть через журналы консоли в setName () и ngAfterViewInit (), что profileName определенно устанавливается во время setName (), но уже снова стал "" ngAfterViewInit ().Есть ли причина, по которой это могло бы произойти?

Класс AccountPage

  export class AccountsPage {

  public profileList = new Array<ProfileDataComponent>();

  constructor(public navCtrl: NavController,
          public navParams: NavParams,
          private alertCtrl: AlertController) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad AccountsPage');
  }

  addProfile()
  {
    let alert = this.alertCtrl.create({
    title: 'Add Profile',
    inputs: [
      {
        name: 'name',
        placeholder: 'Profile Name'
      },
    ],
  buttons: [
  {
    text: 'Cancel',
    role: 'cancel',
    handler: data => {
      console.log('Cancel clicked');
    }
  },
  {
    text: 'Add',
    handler: data => {
      //Declare New Profile
      var newProfile = new ProfileDataComponent();
      newProfile.setName(data.name);

      //Create new list
      var newArray: ProfileDataComponent[] = new Array(this.profileList.length + 1);

      //Copy old list to new one
      for (var i = 0; i < this.profileList.length; i++)
      {
        newArray[i] = this.profileList[i];
      }
        newArray[newArray.length - 1] = newProfile;
        this.profileList = newArray;
    }
  }
]
  });
 alert.present();
  }
}

Класс компонента ProfileData

export class ProfileDataComponent {

  @ViewChild('profileName') nameElement;

  profileName: string = "";

  constructor()
  {

  }

  ngAfterViewInit()
  {
    this.nameElement.textContent = this.profileName;
    console.log('Set Name: ' + this.profileName);
  }

  setName(newName: string)
  {
    this.profileName = newName;
    console.log('Setting Name: ' + this.profileName);
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...