Issue printing api weather data too a html page - html

I am trying too code a very basic Weather app that pulls data from this API https://openweathermap.org/api. When I click the link too go from the home.html file too the weatherData.html nothing is showing up and I cant seem too pinpoint the issue within my code,any help would be appreciated.
<ion-header [translucent]="true">
<ion-toolbar>
<ion-title>
Weather Ireland
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">Weather Ireland</ion-title>
</ion-toolbar>
</ion-header>
<a class="button icon-right ion-plus-round" href="../weather-data/weather-data.page.html">weatherData</a>
</ion-content>
This is the code for the homepage.html which links too the weatherData.html file where the information from the API is supposed to be printed.
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { FormsModule } from '#angular/forms';
import { IonicModule } from '#ionic/angular';
import {WeatherDataService} from '../weather-data.service'
import { WeatherDataPageRoutingModule } from './weather-data-routing.module';
import { WeatherDataPage } from './weather-data.page';
#NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
WeatherDataPageRoutingModule
],
declarations: [WeatherDataPage]
})
export class WeatherDataPageModule {
weather;
constructor(private weatherdataservice: WeatherDataService){}
ngOnInit(){
this.weatherdataservice.getWeather().subscribe((data)=>{
console.log(data);
this.weather = data['weather'];
}
)
}
}
This is the code for the data.html file
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { FormsModule } from '#angular/forms';
import { IonicModule } from '#ionic/angular';
import {WeatherDataService} from '../weather-data.service'
import { WeatherDataPageRoutingModule } from './weather-data-routing.module';
import { WeatherDataPage } from './weather-data.page';
#NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
WeatherDataPageRoutingModule
],
declarations: [WeatherDataPage]
})
export class WeatherDataPageModule {
weather;
constructor(private weatherdataservice: WeatherDataService){}
ngOnInit(){
this.weatherdataservice.getWeather().subscribe((data)=>{
console.log(data);
this.weather = data['weather'];
}
)
}
}
this is the code for the data.module.ts file
import { Injectable } from '#angular/core';
import { HttpClient} from '#angular/common/http'
#Injectable({
providedIn: 'root'
})
export class WeatherDataService {
APIKey = 'fec30507acb533f670080ab3174f226f';
constructor(private httpClient: HttpClient) { }
public getWeather(){
return this.httpClient.get('api.openweathermap.org/data/2.5/weather?lat={53.87}&lon={8.63}&appid={fec30507acb533f670080ab3174f226f}')
}
}
this is the code for the data.services ts file.

From below code, seems like you wanted to redirect to some page though you have response from the api. Instead of link redirection, try to create new page and pass response.
<ion-header [translucent]="true">
<ion-toolbar>
<ion-title>
Weather Ireland
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">Weather Ireland</ion-title>
</ion-toolbar>
</ion-header>
**<a class="button icon-right ion-plus-round" href="../weather-data/weather-data.page.html">weatherData</a>**
</ion-content>
In WeatherDataPage try using NavController
declare, navCtrl: NavController in constructor and use this.navCtrl.push(component_name, {whetherData: this.weather});
And after navigating to new component page use, NavParams. Declare navParams: NavParams and use navParams.get("whetherData");

Related

Firebase Realtime Database Ionic returning null

I'm trying to incorporate a Firebase database into my Ionic application using Angular. I was following the instructions laid out on the AngularFire github page (https://github.com/angular/angularfire2) but when I actually run the application I keep getting a null response. I also am confused on the input of the of the db.object method (db.object('item')). What does 'item' actually represent?
This is my app.component.ts file
import { Component } from '#angular/core';
import { Platform } from '#ionic/angular';
import { SplashScreen } from '#ionic-native/splash-screen/ngx';
import { StatusBar } from '#ionic-native/status-bar/ngx';
import { MenuController } from '#ionic/angular';
import { AngularFireDatabase } from '#angular/fire/database';
import { Observable } from 'rxjs';
#Component({
selector: 'app-root',
templateUrl: 'app.component.html'
})
export class AppComponent {
item: Observable<any>;
example: Observable<any>;
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private menu: MenuController,
private db: AngularFireDatabase
){
this.item = db.object('item').valueChanges();
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
close(){
this.menu.close();
}
}
This is my app.component.html file
<ion-app>
<ion-menu side="start" menuId="first">
<ion-header>
<ion-toolbar color="primary">
<ion-title>Hi</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item [routerLink]="['/orders']" routerLinkActive="active" (click)="close()" >My Orders</ion-item>
<ion-item [routerLink]="['/apps']" routerLinkActive="active" (click)="close()">My</ion-item>
<ion-item [routerLink]="['/pres']" routerLinkActive="active" (click)="close()">My</ion-item>
<ion-item>{{ (item | async)?.name }}</ion-item>
</ion-list>
<li class="text" *ngFor="let item of items | async">
{{ item | json }}
</li>
</ion-content>
</ion-menu>
<ion-router-outlet main></ion-router-outlet>
</ion-app>
This is my app.module.ts file
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { RouteReuseStrategy } from '#angular/router';
import { FormsModule } from '#angular/forms';
import { IonicModule, IonicRouteStrategy } from '#ionic/angular';
import { SplashScreen } from '#ionic-native/splash-screen/ngx';
import { StatusBar } from '#ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { AngularFireModule } from '#angular/fire';
import { environment } from '../environments/environment';
import { AngularFirestoreModule } from '#angular/fire/firestore';
import { AngularFireDatabaseModule } from '#angular/fire/database';
import { AngularFireAuthModule } from '#angular/fire/auth';
#NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, FormsModule, IonicModule.forRoot(), AppRoutingModule, AngularFireModule.initializeApp(environment.firebase), AngularFireDatabaseModule, AngularFireAuthModule],
providers: [
StatusBar,
GooglePlus,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
Image of my Firebase database. This is what can be seen right underneath my app name: https://pasteboard.co/HTxY5eC.png
Hi Kiran and welcome to SO. You went way overkill on how much information you provided, but rather be safe than sorry - right?
In the example you're looking at item is the node in the database. I take it you're also fairly new to Firebase, so I won't assume anything.... Have you played around with the Firebase console yet? (https://console.firebase.google.com/project/PROJECT_NAME/database/PROJECT_NAME/data) The view that allows you to see the contents of your Real-Time Database? Play around in there and add a few "nodes" in the database. They have nicely coordinated colors for when you add, edit, and remove data... pretty cool, right? Now erase your test nodes and create a node named "item", and add a few child nodes.
In your code this.item = db.object('item').valueChanges(); listens to the changes in the information stored in the item node of your database... and sticks it into the this.item variable. Your HTML file loops over any contents of the this.item variable and displays it via *ngFor="let item of items | async
So long story short - your original question... item isn't anything special - make it whatever you call your database node. You're getting null because right now you are missing a node in your DB with a matching name of item

Runtime Error = object is not a function

I'm developing my first aplication with Ionic, and would like to display the firebase data at home.html, but when I start the serve show me this message in browser:
object(...) is not a function
I create this code at home.ts:
import { Component } from '#angular/core';
import { NavController, IonicPage } from 'ionic-angular';
import { ShoppingListService } from '../../services/shopping-list/shopping-list.service';
import { AjaxObservable } from 'rxjs/observable/dom/AjaxObservable';
import { Observable } from 'rxjs/Observable';
import { Item } from '../../models/item/item.model';
#IonicPage()
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
shoppingList$ : Observable <Item[]>;
constructor(public navCtrl: NavController, private shopping:ShoppingListService) {
this.shoppingList$ = this.shopping
.getShoppingList() //pega a lista do banco de dados
.snapshotChanges() //chave e valor das infos do bd
.map(changes => {
return changes.map(c => ({
key: c.payload.key,
...c.payload.val(),
}));
});
}
}
After this, I create the home.html:
<ion-content padding>
<ion-list>
<ion-list-header>
Items
</ion-list-header>
<ion-item *ngFor="let item of shoppingList$ | async">
{{item.nomecontratante}}
</ion-item>
</ion-list>
</ion-content>
Thanks for help me!

Show specific data for the clicked element using popover

when I retrieve data from a json file, in the *ngFor displays all the values in the popover, but I need a specific popover to display based only on the data for selected/clicked weapon. Here is my code any help would be greatly appreciated. Thank you again for your help
Home
import { Component } from '#angular/core';
import { NavController, ViewController, PopoverController, Events} from 'ionic-angular';
import { RestProvider } from './../../providers/rest/rest';
import { PopupPage } from './../../pages/popup/popup';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
weapons: any;
errorMessage: string;
constructor(public navCtrl: NavController, public rest: RestProvider,
public popoverCtrl: PopoverController) {
}
ionViewDidLoad() {
this.getweapons();
}
getweapons() {
this.rest.getweapons()
.subscribe(
weapons => this.weapons = weapons,
error => this.errorMessage = <any>error);
}
presentPopover(myEvent)
{
let popover = this.popoverCtrl.create(PopupPage);
popover.present({
ev: myEvent
});
}
}
home.html
<ion-content>
<ion-searchbar [(ngModel)]="terms"></ion-searchbar>
<ion-item>
</ion-item>
<ion-list>
<button ion-item (click)="presentPopover($event)">
<ion-item *ngFor="let c of weapons?.weapon_category?.weapons | search : terms">
<h2>{{c.name}}</h2>
</ion-item>
</button>
</ion-list>
</ion-content>
popup.ts
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams, ViewController, Events} from 'ionic-angular';
import { RestProvider } from './../../providers/rest/rest';
import { HomePage } from './../../pages/home/home';
/**
* Generated class for the PopupPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-popup',
templateUrl: 'popup.html',
})
export class PopupPage {
rangeSettings = 20;
weapons: any;
errorMessage: string;
constructor(public navCtrl: NavController, public navParams: NavParams, public viewCtrl: ViewController, public rest: RestProvider) {
alert('inside the popup');
}
close() {
this.viewCtrl.dismiss();
}
getweapons() {
this.rest.getweapons()
.subscribe(
weapons => this.weapons = weapons,
error => this.errorMessage = <any>error);
}
ionViewDidLoad() {
this.getweapons();
}
}
popup.html
<ion-header>
<ion-navbar>
<ion-title>popup</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item *ngFor="let c of weapons?.weapon_category?.weapons">
<h2>{{c.damage.base}}</h2>
<h2>{{c.damage.chest0}}</h2>
<h2>{{c.damage.chest1}}</h2>
<h2>{{c.damage.chest2}}</h2>
<h2>{{c.damage.chest3}}</h2>
<h2>{{c.damage.head1}}</h2>
<h2>{{c.damage.head2}}</h2>
<h2>{{c.damage.head3}}</h2>
</ion-item>
<ion-item>
<ion-range min="0" max="80" [(ngModel)]="rangeSettings" color="danger" pin="true" snaps="true" disabled=true></ion-range>
</ion-item>
</ion-list>
</ion-content>
Popover doesn't need to hit the REST call again. You can pass the chosen weapon to the popover as a parameter.
Change your function to accept a weapon (make sure you change the code in the HTML too)
presentPopover(myEvent, weapon)
And send it to the popover controller this way:
this.popoverCtrl.create(PopupPage, weapon);
Now in your popup.ts, decleare a weapon object in your class,
weapon : any;
and grab the weapon from the navParams in your constructor
this.weapon = this.navParams.data;
Change your <ion-item> in popup.html to display the selected one.
<ion-item>
{{weapon.damage.base}}
...
</ion-item>

Display image json object into ionic list ( v2)

I have a json object that contains id of an image and its url in the server , in order to display it in an ion-list i try the code below but the image doesn't appear.
About.html
<ion-content padding>
<ion-list>
<ion-item *ngFor="let post of posts">
<img [src]="post.pic"/>
</ion-item>
</ion-list>
</ion-content>
here is my json object :
{"ListeImages":[{"id":"44","pic":"https:\/\/stationpfe.000webhostapp.com\/projet\/uploads\/1494201248244.jpg"}],"success":1}
About.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import {Imageservice} from '../../providers/imageservice';
import { Http } from '#angular/http';
#Component({
selector: 'page-about',
templateUrl: 'about.html',
providers:[Imageservice]
})
export class AboutPage {
posts: any;
constructor(public navCtrl: NavController,public im:Imageservice,public http: Http) {
this.getImages();
}
getImages(){
this.im.OneArrive().subscribe(response=>
{
this.posts=response.ListeImages;
});
}
}
imageservice.ts ( provider)
import { Injectable } from '#angular/core';
import { Http } from '#angular/http';
import 'rxjs/add/operator/map';
#Injectable()
export class Imageservice {
constructor(public http: Http) {
console.log('Hello Imageservice Provider');
}
public OneArrive(){
var url='https://stationpfe.000webhostapp.com/projet/SelectListeImages.php';
return this.http.get(url).map(res=>res.json());
}
}
<ion-avatar item-left>
<img [src]="imageUrl+card.image" />
</ion-avatar>
i have stored the image url in .ts file the card.image value is the value that i get from server.
In my case i have a server storage=>http://311.11.111.111:8000/storage<=like this
i specify the storage + the value from api to display in the
<img [scr]=""/> specified above.
Hope it helps....

DataApi data not found (not display in Html) in Ionic2

I'm developing an application for Ionic 2.I'm taking pictures from JSON, and I list the data in the console.log.However they're not displayed in HTML.Error (GET http://ukopuz.com/[object%20Object] 404 (Not Found) )Where did I go wrong?
data-api.service.ts
import {Injectable} from '#angular/core';
import {Http,Response} from '#angular/http';
#Injectable()
export class DataApi {
private url = 'http://ukopuz.com/api/2';
constructor(private http:Http){
}
getReferenceData(){
return new Promise(resolve =>{
this.http.get(`${this.url}`)
.subscribe(res => resolve(res.json()))
});
}
}
reference.html
<ion-content class="content">
<ion-grid>
<ion-row *ngFor="let ref of reference">
<ion-col col-4 >
<img src="http://ukopuz.com/{{ref}}" >
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
reference.ts
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams,ModalController } from
'ionic-angular';
import {DataApi} from '../../app/dataapi/data-api.service';
import {Http, HttpModule} from '#angular/http';
import {ModalPage} from '../modal/modal';
#IonicPage()
#Component({
selector: 'page-references',
templateUrl: 'references.html',
})
export class ReferencesPage {
reference : any;
constructor(public navCtrl: NavController, public navParams:
NavParams,public dataApi:DataApi,
public http:HttpModule,public modalCtrl:ModalController) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad ReferencesPage');
this.dataApi.getReferenceData().then(data =>
this.reference = data);
console.log("data:"+ this.reference );
console.log("viewload");
}
}
data.json
[
{
"3": "/img/ref/adana.png",
"4": "/img/ref/ajans.png",
"5": "/img/ref/akp.jpg",
"6": "/img/ref/akp.png",
"7": "/img/ref/akpgenclik.png",
"8": "/img/ref/ankara.png",
"9": "/img/ref/arnavut.png",
"10": "/img/ref/aydin.png"
}
]
I have checked your return json from API, it is not returning any data due to this you are getting any response in your .html page
Check the API page