0

I have an app created using Nativescript with Angular that uses a TabView.

The action bar has a refresh button which I want to be able to refresh the data on the whichever route is visible.

I've tried using the answer from Angular2 - Interaction between components using a service

but I'm getting this error when it's clicked

this.sharedService.componentDashFn is not a function. (In 'this.sharedService.componentDashFn()', 'this.sharedService.componentDashFn' is undefined)

share-service.ts

import { Injectable } from "@angular/core";
@Injectable()
export class SharedService {

    componentDashFn: Function;
    componentPropFn: Function;
    componentHouseFn: Function;
    componentMoreFn: Function;

    constructor() { }
}

home.html

<ActionBar title="Genkan">
<ActionItem>
 <Image src="res://logo_fw"></Image>
</ActionItem>
   <ActionItem [visibility]="showRefresh ? 'visible' : 'collapse'" text="Refresh" (tap)="refresh()" android.systemIcon="ic_menu_refresh" ios.systemIcon="13" ios.position="right"></ActionItem>
</ActionBar>

<TabView #element [(ngModel)]="tabSelectedIndex" (selectedIndexChange)="onIndexChanged($event)" selectedColor="#d8292f">
    <ng-template tabItem title="Dashboard" iconSource="{{dashActive === true ? 'res://dashboard' : 'res://dashboard'}}">
        <StackLayout>
    <router-outlet name="dashoutlet"></router-outlet>
        </StackLayout>
    </ng-template>
    <ng-template tabItem title="Properties" iconSource="{{propActive === true ? 'res://properties' : 'res://properties'}}">
        <StackLayout>
    <router-outlet name="propoutlet"></router-outlet>
        </StackLayout>
    </ng-template>
    <ng-template tabItem title="Housekeeping" iconSource="{{trustActive === true ? 'res://trust' : 'res://trust'}}">
        <StackLayout>
    <router-outlet name="trustoutlet"></router-outlet>
        </StackLayout>
    </ng-template>
    <ng-template tabItem title="More" iconSource="{{moreActive === true ? 'res://more' : 'res://more'}}">
        <StackLayout>
    <router-outlet name="moreoutlet"></router-outlet>
        </StackLayout>
    </ng-template>
</TabView>

home.component.ts

import { SharedService } from "../../shared/share-service";
@NgModule({
  imports: [
    NativeScriptModule,
    NativeScriptFormsModule
  ],
  declarations: [HomeComponent],
  bootstrap: [HomeComponent]
})
@Component({
  selector: "home",
  templateUrl: "pages/home/home.html",
  styleUrls: ["pages/home/home-common.css", "pages/home/home.css"],
  providers: [GenkanService, BookingListService, SharedService]
})
export class HomeComponent implements OnInit, PipeTransform {
constructor(private sharedService: SharedService, private bookingListService: BookingListService, private http: Http, private nav: RouterExtensions, private zone: NgZone, private router: Router, private data: Data) {    }

......
  refresh(){
    switch(this.tabSelectedIndex){
        case 0: //dashboard
             this.sharedService.componentDashFn(); 
        break;
        case 1: //properties
             // this.sharedService.componentPropFn(); 
        break;
        case 2: //housekeeping
             // this.sharedService.componentHouseFn(); 
        break;
        case 3: //more
             // this.sharedService.componentMoreFn(); 
        break;
    }
  }
}

dashboard.component.ts

import { SharedService } from "../../shared/share-service";
@NgModule({
  imports: [
    NativeScriptModule,
    NativeScriptFormsModule
  ],
  declarations: [DashboardComponent],
  bootstrap: [DashboardComponent]
})
@Component({
  selector: "dashboard",
  templateUrl: "pages/dashboard/dashboard.html",
  styleUrls: ["pages/dashboard/dashboard-common.css", "pages/dashboard/dashboard.css"],
  providers: [GenkanService, BookingListService, MyUtils, SharedService]
})
export class DashboardComponent implements OnInit, PipeTransform {
    constructor(private sharedService: SharedService, private bookingListService: BookingListService, private http: Http, private nav: RouterExtensions, private genkanService: GenkanService, private myutil: MyUtils, private zone: NgZone, private router: Router, private data: Data) {
        this.sharedService.componentDashFn = this.refresh;
        console.log(this.sharedService.componentDashFn);
    }
    refresh(){
        console.log('dashboard refresh');
        this.data.storage.dashData = false;
        this.todays_arrivals = 0;
        this.todays_departures = 0;
        this.current_bookings = 0;
        this.total_forward_bookings = 0;
        this.loadData();
    }



loadData(){
        console.log('loadData');
        this.isLoading = true;
        this.currentdate = new Date();
        let client = JSON.parse(ApplicationSettings.getString("client_loggin"));
        console.dir(this.myutil);
        this.welcome = 'Welcome ' + client.user + ' | ' + this.currentdate.getDate() + ' ' + this.myutil.monthText[this.currentdate.getMonth()] + ' ' + this.currentdate.getFullYear();
        let cmonth = this.currentdate.getMonth()+1;
        let cdate = this.currentdate.getDate();
        let month_s = cmonth + '';
        let date_s = cdate + '';
        if(cmonth < 10) { month_s = '0' + cmonth;}
        if(cdate < 10) { date_s = '0' + cdate;}
        let formated_today = this.currentdate.getFullYear() + '-' + month_s + '-' + date_s;
        let formated_lastyear = (this.currentdate.getFullYear()-1) + '-' + month_s + '-' + date_s;
        let month_today = this.currentdate.getFullYear() + '-' + month_s;
        let month_lastyear = (this.currentdate.getFullYear()-1) + '-' + month_s;
        let year_today = this.currentdate.getFullYear() + '';
        let year_lastyear = (this.currentdate.getFullYear()-1) + '';
        this.getBookingData(client, year_today, 'get_bookings_summary', 'this_year', false);
        this.getBookingData(client, year_lastyear, 'get_bookings_summary', 'last_year', false);
        this.getBookingData(client, month_today, 'get_bookings_summary', 'this_year', false);
        this.getBookingData(client, month_lastyear, 'get_bookings_summary', 'last_year', false);
    switch(this.current_display){
        case 'month':
            formated_today = this.currentdate.getFullYear() + '-' + month_s;
            formated_lastyear = (this.currentdate.getFullYear()-1) + '-' + month_s;
            break;
        case 'year':
            formated_today = this.currentdate.getFullYear() + '';
            formated_lastyear = (this.currentdate.getFullYear()-1) + '';
            break;
        case 'today':
        default:
            formated_today = this.currentdate.getFullYear() + '-' + month_s + '-' + date_s;
            formated_lastyear = (this.currentdate.getFullYear()-1) + '-' + month_s + '-' + date_s;
            break;
    }

        this.getBookingData(client, formated_today, 'get_bookings_summary', 'this_year');
        this.getBookingData(client, formated_lastyear, 'get_bookings_summary', 'last_year');
        this.getBookingData(client, formated_today, 'get_current_bookings', 'current_bookings');
        this.getBookingData(client, formated_today, 'get_forward_bookings', 'forward_bookings');

    }

}
Dataman In Time
  • 23
  • 1
  • 12
  • Please show your code (template, component and service) – Clément Denoix Nov 16 '17 at 04:30
  • I removed the ShardService from the list of providers and the error this.sharedService.componentDashFn' is undefined is no longer. But now i get JS ERROR TypeError: this.loadData is not a function. (In 'this.loadData()', 'this.loadData' is undefined Is there a way to make the loadData() function from the dashboard available from the homeComponent? – Dataman In Time Nov 17 '17 at 04:01
  • Why not add the `loadData()` method on your `sharedService` ? – Clément Denoix Nov 17 '17 at 04:05
  • I have done that now, but it appears that 'this.myutil' is undefined within the loadData() function when the refresh is clicked, although it is defined on the initial load. – Dataman In Time Nov 17 '17 at 06:45
  • Is there an easy way to get the values to reset in the refresh function? Currently if I add the variables to the shareService then the values aren't reflected in the dashboard such as an isLoading Boolean which shows the activity indicator. – Dataman In Time Nov 20 '17 at 03:34

0 Answers0