Hello,
I was trying to refer to a static value in a provider from a web page (html file)
I first defined some static properties in a provider . please see below code snippets , I excerpt it for clearance
import { Injectable } from '@angular/core';
import {Post} from "fbsj";
@Injectable()
export class TransSender {
public static type:number = Post.Sender;
public static count:number = 0;
public static pushed:number = 0;
... ...
constructor(){};
public static setter(count:number, pushed:number, read:number, saved:number, rated:number, followed:number, blocked:number, forwarded:number) {
TransSender.count = count;
TransSender.pushed = pushed;
... ...
}
}
then I enlisted the provider in ionicBootstrap:
import {Component, ViewChild} from '@angular/core';
import {ionicBootstrap, Platform, MenuController, Nav} from 'ionic-angular';
... ...
import {TransSender} from './providers/trans-sender/trans-sender';
@Component({
templateUrl: 'build/app.html',
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
// make HelloIonicPage the root (or first) page
rootPage: any = HelloIonicPage;
pages: Array<{title: string, component: any}>;
constructor(
private platform: Platform,
private menu: MenuController
) {
this.initializeApp();
// set our app's pages
this.pages = [
{ title: 'My First List', component: ListPage },
{ title: 'Anonymous', component: SenderPage },
{ title: 'Main Page', component: HelloIonicPage }
];
}
initializeApp() {
this.platform.ready().then(() => {
StatusBar.styleDefault();
... ...
});
}
openPage(page) {
this.menu.close();
this.nav.setRoot(page.component);
}
}
ionicBootstrap(MyApp,[User,TransSender]);
then I injected it into the client class in “general.ts”
import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
... ...
import {TransSender} from "../../providers/trans-sender/trans-sender";
@Component({
templateUrl: 'build/pages/general/general.html',
})
export class GeneralPage {
selectedItem: any;
private type:number;
private count:number;
private pushed:number;
... ...
constructor(private nav: NavController, navParams: NavParams, sender: TransSender) {
this.selectedItem = navParams.get('item');
... ...
this.pushed = TransSender.pushed;
}
... ...
}
then I referred to the static value in the corresponding html file (general.html) as the following
<ion-content>
<div *ngIf="selectedItem" class="selection">
<b>{{selectedItem.title}}</b>
<ion-icon name="{{selectedItem.icon}}"></ion-icon>
<div>
count <b>{{TransSender.count}}</b>
</div>
<div>
pushed <b>{{pushed}}</b>
</div>
</div>
</ion-content>
when I run “ionic serve -s -l -c”, I got the following errors
... ...
erve /build/pages/general/general.html
3 364765 group EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in build/pages/general/general.html:18:18
ORIGINAL EXCEPTION: TypeError: Cannot read property 'count' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'count' of undefined
at DebugAppView._View_GeneralPage2.detectChangesInternal (GeneralPage.template.js:429:73)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14832:15)
at DebugAppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14937:45)
at DebugAppView.AppView.detectContentChildrenChanges (http://localhost:8100/build/js/app.bundle.js:14850:20)
at DebugAppView._View_GeneralPage0.detectChangesInternal (GeneralPage.template.js:155:8)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14832:15)
at DebugAppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14937:45)
at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:8100/build/js/app.bundle.js:14858:20)
at DebugAppView.AppView.detectChangesInternal (http://localhost:8100/build/js/app.bundle.js:14843:15)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14832:15)
ERROR CONTEXT:
[object Object]
4 364766 error EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in build/pages/general/general.html:18:18
ORIGINAL EXCEPTION: TypeError: Cannot read property 'count' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'count' of undefined
at DebugAppView._View_GeneralPage2.detectChangesInternal (GeneralPage.template.js:429:73)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14832:15)
at DebugAppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14937:45)
at DebugAppView.AppView.detectContentChildrenChanges (http://localhost:8100/build/js/app.bundle.js:14850:20)
at DebugAppView._View_GeneralPage0.detectChangesInternal (GeneralPage.template.js:155:8)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14832:15)
at DebugAppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14937:45)
at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:8100/build/js/app.bundle.js:14858:20)
at DebugAppView.AppView.detectChangesInternal (http://localhost:8100/build/js/app.bundle.js:14843:15)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14832:15)
ERROR CONTEXT:
[object Object]
5 364767 error STACKTRACE:
6 364767 error Error: Uncaught (in promise): EXCEPTION: Error in build/pages/general/general.html:18:18
ORIGINAL EXCEPTION: TypeError: Cannot read property 'count' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'count' of undefined
at DebugAppView._View_GeneralPage2.detectChangesInternal (GeneralPage.template.js:429:73)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14832:15)
at DebugAppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14937:45)
at DebugAppView.AppView.detectContentChildrenChanges (http://localhost:8100/build/js/app.bundle.js:14850:20)
at DebugAppView._View_GeneralPage0.detectChangesInternal (GeneralPage.template.js:155:8)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14832:15)
at DebugAppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14937:45)
at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:8100/build/js/app.bundle.js:14858:20)
at DebugAppView.AppView.detectChangesInternal (http://localhost:8100/build/js/app.bundle.js:14843:15)
at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:14832:15)
ERROR CONTEXT:
[object Object]
at resolvePromise (http://localhost:8100/build/js/zone.js:538:32)
at http://localhost:8100/build/js/zone.js:574:18
at ZoneDelegate.invokeTask (http://localhost:8100/build/js/zone.js:356:38)
at Object.onInvokeTask (http://localhost:8100/build/js/app.bundle.js:12336:42)
at ZoneDelegate.invokeTask (http://localhost:8100/build/js/zone.js:355:43)
at Zone.runTask (http://localhost:8100/build/js/zone.js:256:48)
at drainMicroTaskQueue (http://localhost:8100/build/js/zone.js:474:36)
at XMLHttpRequest.ZoneTask.ZoneTask.cancelFn.invoke (http://localhost:8100/build/js/zone.js:426:22)
7 364768 groupEnd
the only way I can do get over is to define a local property in the client class ( general.ts), and assign the value in the in the constructor. and refer to the local property in the html file
you may see the different handling of “count” and “pushed” in the above code spinets in “general.ts” and “general.html”
please advise
thanks