角度2变量未定义 - 离子

时间:2017-04-28 05:23:28

标签: angular typescript

我的html中有这个

<ion-col col-3 align="right">
          <ion-item>
            <ion-label>Show as</ion-label>
            <ion-select [ngModel]="SelectedView" (ngModelChange)="onViewChange($event)>
              <ion-option value="List">List</ion-option>
              <ion-option value="Map">Map</ion-option>
            </ion-select>
          </ion-item>
        </ion-col>

并在我的customer.ts文件中

export class customerpage implements OnInit {


      constructor(public modalCtrl: ModalController,
        public popoverCtrl: PopoverController,
        private _customerservice: CustomersService,
        public navCtrl: NavController,
        public navParams: NavParams) {

        this.ListPositions = [];
        this.customersArray = [];
        this.customersView = [];
      }

      ngOnInit() {


        this.SelectedCategory = "";

        this.customersArray = [];
        this.customersView = [];

        setTimeout(() => {

          this.GetCustomers();
        }, 500);

      }

      GetCustomers() {
        const arrayofroles$ = this._customerservice.GetAllCustomers();
        arrayofroles$.subscribe(res => {
          this.customersArray = res; //Initially both arrays are the same
          this.customersView = this.customersArray;

        },
          err => { },
          () => {

            this.loadingCustomers = false;
          });

      }


      ionViewDidLoad(): void {     

      }

      onViewChange(event: any, Customers: any) {

         this.initMap(Customers);


      }


      initMap(Customers: any) {

          this.customersView; // UNDEFINED!
      }

}

最初,在nginit上,我正在从数据库加载customersview。来自html的OnViewchange我正在调用initmap函数并试图访问cutomersview,但它给了我未定义的本地变量。

1 个答案:

答案 0 :(得分:0)

一种解决方案可能是:您似乎有数据同步问题。

// return subscription from this method
getCustomers() {
        const customerSubscription = this._customerservice.GetAllCustomers();
        return customerSubscription;
}


onViewChange(event: any) {
         this.getCustomers()
         .subscribe((customers: any) => {
             this.initMap(customers);
         }); 
}