How to get input text value in ionic - html

I'm trying to get input text value and store it in a variable named input in ionic. But I tried and failed. Can anyone please tell me what I have faulty done?
This is my HTML
<ion-content>
<ion-list>
<ion-item>
<ion-label stacked>display</ion-label>
<ion-input type="text" text-right id="input" ></ion-input>
</ion-item>
</ion-list>
</ion-content>
and this is my home.ts in ionic
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController) {
var input=document.getElementById('input').nodeValue;
document.getElementById('seven').innerHTML=input;
}
}

Actually you seems to be using angular not angularjs, use [(ngModel)]
<ion-input type="text" [(ngModel)]="name" text-right id="input" ></ion-input>
and inside the component,
name:string;
so whenever you need the value , you can use.
console.log(this.name);

<ion-content>
<ion-list>
<ion-item>
<ion-label stacked>display</ion-label>
<ion-input type="text" text-right id="input" [(ngModel)]="inputValue"></ion-input>
</ion-item>
</ion-list>
</ion-content>
// ===
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
inputValue: string = "";
constructor(public navCtrl: NavController) {}
someFunction() {
// here you can use the 'this.inputValue' and get the value of the ion-input
}
}
we use the two way binding the value of ion-input with the class member inputValue,
about ngModel
whan you need to access on the value of the input, check the value of inputValue.
here you can see an exemple I wrote on StackBlitz
Two-way binding is a combination of both property binding and event binding as it is a continuous synchronization of data/values from presentation layer to component and from component to the presentation layer.
Since this is a two way binding we have to use both the brackets - [ ( ) ]. Also ngModel is a directive which is used to bind the data both ways.

In Ionic 5 - The way to get the value of an ion-input value when focusing:
<ion-input (ionFocus)="onFocusPlace($event)"></ion-input>
onFocusPlace(event){
this.value = event.target.value;
}

Related

How do I make an angular component render in other component?

I've made an ionic and angular project, but I'm having some difficulties making it work, here is the sourcecode, The main issues that I'm having is using the navbar component within the innsjekk and utsjekk components. I've added the selector and there isn't any errors but it doesn't render. Also in the popover component, I added a routerLink to the profile component but nothing happens when I click it.
popover.component.html:
<ion-list>
<ion-item>
<ion-icon name="person-circle-outline"></ion-icon>
<ion-button routerLink="/profil" expand="block" fill="clear" shape="round">
<ion-label>Profil</ion-label>
</ion-button>
</ion-item>
<ion-item *ngIf="isAdmin">
<ion-icon name="settings-outline"></ion-icon>
<ion-button (click)="admin()" expand="block" fill="clear" shape="round">
<ion-label>Admin</ion-label>
</ion-button>
</ion-item>
<ion-item>
<ion-icon name="log-out-outline"></ion-icon>
<ion-button (click)="logut()" expand="block" fill="clear" shape="round">
<ion-label>Log ut</ion-label>
</ion-button>
</ion-item>
</ion-list>
innsjekk.component.html:
<ion-header [translucent]="true">
<app-navbar></app-navbar>
</ion-header>
<ion-content>
<p>Hello world</p>
</ion-content>
I had forgotten to add the components to the module declarations. Doing so fixed it all, I tried building the project then i got errors explaining the problem.
home.module.ts:
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { IonicModule } from '#ionic/angular';
import { FormsModule } from '#angular/forms';
import { HomePage } from './home.page';
import { NavbarComponent } from './../components/navbar/navbar.component';
import { HomePageRoutingModule } from './home-routing.module';
import { AdminComponent } from '../admin/admin.component';
import { InnsjekkComponent } from '../innsjekk/innsjekk.component';
import { PopoverComponent } from '../components/popover/popover.component';
import { LogoutComponent } from '../components/logout/logout.component';
#NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
HomePageRoutingModule
],
declarations: [HomePage, NavbarComponent, AdminComponent, InnsjekkComponent, InnsjekkComponent, PopoverComponent, LogoutComponent],
exports: [NavbarComponent]
})
export class HomePageModule {}
If you want to pass HTML as parameter to component, there are a lot of ways, with benefits and limitations. Mostly different:
Content projection. Search for <ng-content>. (most simple but has huge limitations)
Pass template as parameter. Search for #ContentChildren, TemplateRef, ViewContainerRef, structural directives. (flexible)
Pass component's type as #Input and render it inside another (using ViewContainerRef.createComponent, [ngComponentOutlet], etc.). (looks dirty)
They require a bit helping code. There can be some 'perversions' with them. Сhoice depends on many reasons.
share

*ngIf doesn't work with ng-template (ionic 5/angular 9)

Here is my component.page.ts file
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-domestic',
templateUrl: './domestic.page.html',
styleUrls: ['./domestic.page.scss'],
})
export class DomesticPage implements OnInit {
opchoice: string;
constructor() { }
ngOnInit() {
this.opchoice = "From";
console.log("OnInit opchoice? " + this.opchoice);
}
onFromToChange (ev: any) {
console.log("OnFromToChange");
this.opchoice = ev.detail.value;
console.log("opchoice? >" + this.opchoice + "<");
}
}
and the HTML file.
<ion-content>
<ion-segment value="From" (ionChange)="onFromToChange($event)">
<ion-segment-button value="From">Pickup From</ion-segment-button>
<ion-segment-button value="To">Deliver To</ion-segment-button>
</ion-segment>
<div *ngIf="opchoice=='From'; then ShowFrom else ShowTo">
<ng-template #ShowFrom class="ion-text-wrap ion-no-padding ion-no-margin">
<ion-item>Domestic - Ship From</ion-item>
</ng-template>
<ng-template #ShowTo class="ion-text-wrap ion-no-padding ion-no-margin">
<ion-item>Domestic - Ship To</ion-item>
</ng-template>
</div>
</ion-content>
However, the page doesn't display anything (it is empty) whether I click on the "Pickup From" or "Deliver To" Tabs/buttons.
Is there something I am doing wrong or have not understood about ng-template or ngIf? As you can see, I am printing the value of the opchoice variable and it prints correctly for each type of click. I have tried putting the ngIf in the ion-item, in an ion-grid and many other combinations, but none work.
I am using Ionic 5 with Angular 9. Any help is appreciated.
As I said before, you have to put the ng-templates at the same level. For me the cleanest solution is:
<div class="ion-text-wrap ion-no-padding ion-no-margin">
<div *ngIf="opchoice=='From'; else ShowTo">
<ion-item>Domestic - Ship From</ion-item>
</div>
<ng-template #ShowTo>
<ion-item>Domestic - Ship To</ion-item>c
</ng-template>
</div>

ion-select-option not choosing selected option when it is specified

I am currently using ionic 4 and I am using the ion-select and ion-select-option tags in html side.
After looking at the documentations when I try to use the selected=true in the ion-select-option it doesn't default to that option chosen. Is there anything that i'm missing or doing wrong?
Here is how my code looks like in the HTML side. I only binded the ngModel on the ts side and nothing else
<ion-select class="ion-select" [(ngModel)]="dialCode">
<ion-select-option value="+1" selected=true>+1</ion-select-option>
<ion-select-option value="+852">+852</ion-select-option>
<ion-select-option value="+86">+86</ion-select-option>
</ion-select>
The issue is that you're binding the ion-select to the dialCode property
... [(ngModel)]="dialCode" ...
So instead of using selected=true, you need to initialize that property with the value that you want to be shown by default. So in your component you could do something like this for example:
// Angular
import { Component } from "#angular/core";
#Component({
selector: "app-home",
templateUrl: "home.page.html",
styleUrls: ["home.page.scss"]
})
export class HomePage implements OnInit {
public dialCode: string = '+1'; // <--- Initialize the property
constructor(...) {}
// ...
}
And then in the view:
<ion-select class="ion-select" [(ngModel)]="dialCode">
<ion-select-option value="+1">+1</ion-select-option>
<ion-select-option value="+852">+852</ion-select-option>
<ion-select-option value="+86">+86</ion-select-option>
</ion-select>

getting errors while displaying json format list in html it cannot be displayed

I am facing some problems while displaying the json formatted data in html file. It work successfully but list is not displayed. This in my code...
import { Component } from '#angular/core';
import { Platform } from 'ionic-angular';
import { Hotspot, Network } from 'ionic-native';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public platform: Platform){
this.platform = platform
}
List(){
Hotspot.scanWifi().then((networks:Array<Network>)=>{
console.log(networks);
});
}
}
This is my HTML File
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button color="Primary" (click)="List()">ScanWifi</button>
<div *ngFor="let network of networks">
<ion-list>
<ion-item>
<h2>{{network}}</h2><br>
</ion-item>
</ion-list>
</div>
</ion-content>
You are just console logging your result, your current code:
List(){
Hotspot.scanWifi().then((networks:Array<Network>)=>{
console.log(networks);
});
You need to declare a local variable networks so that you can use it in the view as you have:
So your code should look something like this:
networks;
List(){
Hotspot.scanWifi().then((networks:Array<Network>)=>{
this.networks = networks;
});
so that you can refer to your networks in your view:
<div *ngFor="let network of networks">
EDIT: I first wrongfully "assumed" that Hotspot needed to be injected into the constructor, but by doing some research, I found out that Hotspot is a native element with methods, and can therefore be called just by Hotspot.scanWifi() as per can be seen here.
You have to set networks array in the class
export class HomePage {
networks:any;
constructor(public platform: Platform){
this.platform = platform
}
List(){
Hotspot.scanWifi().then((networks:Array<Network>)=>{
console.log(networks);
this.networks = networks;//or do forEach and push
});
}
}

Multi Tabs ionic2

i have an ionic2 app and i want to display an ion-tabs with multiple ion-tab from an array. This array i want to populate with a generic tab and pass it an array.
I have this component <multi-tab></multi-tab>:
<ion-tabs tabsPlacement="top" class="multi-tabs">
<ion-tab *ngFor="let tab of tabs" [tabTitle]="tab.title" [root]="tab.component"></ion-tab>
</ion-tabs>
And this is the ts file:
[some imports here]
export class Tab {
title: string;
component: any;
};
const tabs = [{title: 'Snacks', component: Snacks},
{title: 'Drinks', component: Drinks},
{title: 'Frozen', component: Frozen},
{title: 'Custom', component: customTab}];
#Component({
selector: 'multi-tab',
templateUrl: 'multitab.html'
})
export class multiTab {
tabs : Array<Tab>;
constructor(public modalCtrl: ModalController) {
this.tabs = tabs;
}
}
This is my custom tab component:
<ion-content>
<ion-list>
<ion-item *ngFor="let item of items">
<h2>{{item.title}}</h2>
<p>Quantity: {{item.units}}</p>
<ion-icon name="trash" item-right color="indigo100" (click)="onDelete(item)"></ion-icon>
</ion-item>
</ion-list>
<ion-fab center bottom>
<button ion-fab mini color="pink200" (click)="onAdd()"><ion-icon name="add"></ion-icon></button>
</ion-fab>
</ion-content>
And this the ts file:
[some imports]
const CustomItems = [{
'title': 'custom',
'units': 1
}];
#Component({
templateUrl: 'customTab.html'
})
export class customTab {
items : Array<Item>;
constructor(public modalCtrl: ModalController) {
this.items = CustomItems;
}
[some methods]
I want to use in all tabs the customTab and give it an array to initialize (the items array property) But the closest I've ever been is having this error:
Can't resolve all parameters for customTab(?)
when i tried:
constructor(public aux: Array<Item>) {
this.items = aux;
}
There are 2 options, either use the property rootParams (explained in depth here for example) or create a service that is injected in both components.
Both solutions have their merits, the first one is using the mechanism introduced explicitly for that purpose because ion-tabs are custom components and you can't use inputs and outputs. The second solution is the main mechanism of sharing data in angular applications (like a user session) but can seem as overkill for this small task.
There is an example of using rootParams in this commit.