I have this code for an <ion-toggle> in a modal page but when I go to the page it freezes Ionic and I need to shut it down to get it working again. When I leave the binding out, the <ion-toggle> is displayed correct. On the same page is a form that is using ngModel.
<ion-item>
<ion-label>public</ion-label>
<ion-toggle (ionChange)="togglePublic()" [(ngModel)]="newComment.public"></ion-toggle>
</ion-item>
Sombody an idea what the problem can be?
The full html .
<ion-content padding>
<ng-container *ngIf="timer">
<ng-container *ngIf="addingComment">
<ion-card-header text-center class="new-comment">
{{newComment.author}}
</ion-card-header>
<ion-card-content>
<form [formGroup]="commentGroup">
<ion-textarea placeholder="" clearInput formControlName="comment" [(ngModel)]="newComment.message"></ion-textarea>
<ion-item *ngIf="commentGroup.controls['comment'].hasError('minlength') && commentGroup.controls.comment.touched" class="invalid">
<p>The comment needs to be at least 4 characters</p>
</ion-item>
<ion-item *ngIf="commentGroup.controls['comment'].hasError('maxlength') && commentGroup.controls.comment.touched" class="invalid">
<p>The max lengt is 140 characters</p>
</ion-item>
<ion-item *ngIf="commentGroup.controls['comment'].hasError('required') && commentGroup.controls.comment.touched" class="invalid">
<p>A comment is required!</p>
</ion-item>
</form>
<ion-item>
<ion-label>public</ion-label>
<ion-toggle (ionChange)="togglePublic()" [(ngModel)]="newComment.public"></ion-toggle>
</ion-item>
<p class="text-center">{{sToTime(newComment.time)}}</p>
<ion-buttons end>
<button (tap)="cancelComment()" color="danger" ion-button small>cancel</button>
<button (tap)="postComment()" ion-button small>post</button>
</ion-buttons>
</ion-card-content>
</ng-container>
<ion-card *ngFor="let comment of commentsToDisplay" class="comment ">
<ng-container *ngIf="!comment.remove && !comment.addComment">
<ion-card-header class="text-center">
{{comment.author}}
</ion-card-header>
<ion-card-content>
<p>{{comment.message}}</p>
<p class="text-center">{{sToTime(comment.time)}}</p>
</ion-card-content>
</ng-container>
<ng-container *ngIf="comment.remove && !comment.addComment">
<div class="fadeout">
<ion-card-header class="text-center">
{{comment.author}}
</ion-card-header>
<ion-card-content>
<p>{{comment.message}}</p>
<p class="text-center">{{sToTime(comment.time)}}</p>
</ion-card-content>
</div>
</ng-container>
</ion-card>
</ng-container>
</ion-content>
And here is the TS constructor where the this.newComment.public to bind is defined in.
constructor(public viewCtrl: ViewController, public navParams: NavParams, public authService: AuthService, public commentData: CommentData) {
this.isSerie = navParams.get('isSerie');
this.media = navParams.get('media');
this.comments = [];
this.commentsToDisplay = [];
this.commentGroup = new FormGroup({ comment: new FormControl('', Validators.compose([Validators.required, Validators.minLength(4), Validators.maxLength(140)])) });
this.forwardingTimer = Observable.timer(0, 250);
this.newComment = {
message: '',
author: this.authService.currentUser.user_name,
time: this.originalTime,
public: true
}
}
Related
I am having dynamic cards in my ionic project and I want to add dynamic images for different cards.
this is my .html code:
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title *ngFor="let file_uri of row"><p>{{file-uri.name}}</p></ion-title>
</ion-navbar>
</ion-header>
<ion-content class="cards-bg social-cards">
<ion-card *ngFor="let uri of card">
<p ion-button color="light" clear>{{uri.myurl}}hi</p>
<ion-item (click)="OpenContentPage()">
<ion-avatar item-start>
<img src="{{uri.myurl}}">
<p>{{uri.myurl}}</p>
</ion-avatar>
<h2>Marty McFly</h2>
<p>November 5, 1955</p>
</ion-item>
<img src="{{uri.myurl}}" (click)="OpenContentPage()">
<ion-card-content (click)="OpenContentPage()">
<p>hello</p>
</ion-card-content>
</ion-card>
</ion-content>
this is my .ts code:
card: Array<any>; //array of arrays
constructor(public nav: NavController, public navParams: NavParams) {
var temp1 = [{myurl: 'assets/img/advance-card-bttf.png', code: 'i am gandhar'},
];
//console.log(this.navParams.get("temp1"));
this.card = [];
this.card.push(temp1);
this.card.push(temp1);
console.log(this.card);
}
It's giving me dynamic cards but, it is not displaying image and data from object.
Can anyone help?
Remove this brackets [] and it will display image and data. Try like this:
var temp1 = {myurl: 'assets/img/advance-card-bttf.png', code: 'i am gandhar'};
I have an "ion-list" in an "ion-item" from a parent list in a view. But, the child list doesn't display any data. Here's the code i use to diplay the lists :
<ion-list>
<ion-list-header>
{{ 'forms.settings.areas' | translate }}
</ion-list-header>
<ion-item *ngFor="let area of areas">
<ion-list>
<ion-item class="item-thumbnail-left">
<ion-avatar>
<img src="assets/images/areas/{{ area.logo }}" title="{{ area.translations.fr }}" />
</ion-avatar>
<h2>{{ area.translations.fr }}</h2>
</ion-item>
<ion-item>
<ion-label floating>{{ 'forms.settings.area.followDLC' | translate }}</ion-label>
<ion-toggle [(ngModel)]="area.followDLC" [checked]="area.id == 1" color="primary"></ion-toggle>
</ion-item>
<ion-item *ngIf="area.followDLC">
<ion-label floating>{{ 'forms.settings.area.alertBefore' | translate }}</ion-label>
<ion-range min="0" max="7" step="1" snaps="true" color="primary" pin="true" [(ngModel)]="area.alertBefore">
<ion-label range-left>0</ion-label>
<ion-label range-right>7</ion-label>
</ion-range>
</ion-item>
</ion-list>
</ion-item>
</ion-list>
The ion-list-header is correctly rendered, but the other items are blank.
The "areas" structure on which i loop is an array of objects and seems correct :
[{"id":1,"type":"area","translations":{"fr":"Frigo","en":"Fridge"},"stats":{"totalProducts":5,"totalQuantity":5,"stockPrice":null,"alertOnPeremption":0,"alertOnStockLevel":0},"logo":"frigo.png","_id":"1","_rev":"50-55c270db9c0ac62ae8e378e1773a390c","followDLC":true,"alertBefore":7},
{"id":2,"type":"area","translations":{"fr":"Congélateur","en":"Freezer"},"stats":{"totalProducts":5,"totalQuantity":1,"stockPrice":4.99,"alertOnPeremption":0,"alertOnStockLevel":1},"logo":"congelateur.png","_id":"2","_rev":"19-307ca9a1882e4c1781cad5b062cdd920","followDLC":false,"alertBefore":0},
{"id":3,"type":"area","translations":{"fr":"Garde-Manger","en":"Pantry"},"stats":{"totalProducts":11,"totalQuantity":5,"stockPrice":25.48,"alertOnPeremption":0,"alertOnStockLevel":0},"logo":"garde-manger.png","_id":"3","_rev":"118-2746786125e84ae31dd9bfaaecd73118","followDLC":false,"alertBefore":0}]
The screenshot of the result follows :
Someone can help ?
Okay...
Don't know why the first code doesn't render the way i want... But, i change the view this way :
<ion-item-group *ngFor="let area of areas">
<ion-item-divider color="light">
<ion-avatar>
<img src="assets/images/areas/{{ area.logo }}" title="{{ area.translations.fr }}" />
</ion-avatar>
<h2>{{ area.translations.fr }}</h2>
</ion-item-divider>
<ion-item>
<ion-label item-left stacked>{{ 'forms.settings.area.followDLC' | translate }}</ion-label>
<ion-toggle item-right [(ngModel)]="area.followDLC" [checked]="area.followDLC" color="primary"></ion-toggle>
</ion-item>
<ion-item>
<ion-label item-left stacked>{{ 'forms.settings.area.alertBefore' | translate }}</ion-label>
<ion-range item-right min="0" max="7" step="1" snaps="true" color="primary" pin="true" [(ngModel)]="area.alertBefore">
<ion-label range-left>0</ion-label>
<ion-label range-right>7</ion-label>
</ion-range>
</ion-item>
</ion-item-group>
Then, labels and input renders the way i want...
Thx all
I am very new to Ionic 2 and I am unable to change my side menu background color and also the menu-header color of my app. Any help appreciated! Below is the code snippet and the desired result(image). I have numbered the requirements for convenience. Also, it would be very helpful if someone can help with the implementation of drop down option(requirement no. 3). Basically it would contain some sub-list items. Thanks in advance!
<ion-menu [content]="content">
<ion-header no-border>
<ion-toolbar color = "white">
<ion-title class="titletext" style="display:inline-block" >
<div style = "width : 100%; height : 100%; background-color : white">
<div style = "float:left;width:75%">
<h3>MY APP </h3>
</div>
<div style = "float:right;width:25%">
<img src = "assets/icon/reports.PNG" />
</div>
</div>
</ion-title>
<!--<img src="assets/icon/Wemart_Icon.png" alt="logo" height="40px" width = "40px" > -->
</ion-toolbar>
</ion-header>
<ion-content>
<div style = "color : #3DBCC0; width:100%; height : 100%">
<ion-list>
<!--<button menuClose ion-item *ngFor="let p of myPages" (click)="openPage(p)">
<span text-color="my-custom-color2">{{p.title}}</span>
</button>-->
<button ion-item>
<ion-icon name="home" item-left></ion-icon> HOME
</button>
<button ion-item>
<ion-icon name="home" item-left></ion-icon> PORTFOLIO ANALYTICS
</button>
<button ion-item>
<ion-icon name="home" item-left></ion-icon> EXPENSES
</button>
<button ion-item>
<ion-icon name="home" item-left></ion-icon> UTILITY ANALYTICS
</button>
<button ion-item>
<ion-icon name="home" item-left></ion-icon> TERMS OF USE
</button>
<button ion-item>
<ion-icon name="home" item-left></ion-icon> SIGN OUT
</button>
</ion-list>
</div>
</ion-content>
</ion-menu>
<!-- Disable swipe-to-go-back because it's poor UX to combine STGB with side menus -->
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
I am adding variables.scss code below:
// Ionic Variables and Theming. For more info, please see:
// http://ionicframework.com/docs/v2/theming/
$font-path: "../assets/fonts";
#import "ionic.globals";
// Shared Variables
// --------------------------------------------------
// To customize the look and feel of this app, you can override
// the Sass variables found in Ionic's source scss files.
// To view all the possible Ionic variables, see:
// http://ionicframework.com/docs/v2/theming/overriding-ionic-variables/
$toolbar-background: white;
$toolbar-wp-title-text-align : left;
// Named Color Variables
// --------------------------------------------------
// Named colors makes it easy to reuse colors on various components.
// It's highly recommended to change the default colors
// to match your app's branding. Ionic uses a Sass map of
// colors so you can add, rename and remove colors as needed.
// The "primary" color is the only required color in the map.
$colors: (
primary: #387ef5,
secondary: #32db64,
danger: #f53d3d,
light: #f4f4f4,
dark: #222,
color1 : #8FAADC,
color2 : #DAE3F3,
color3: #3DBCC0
);
// App iOS Variables
// --------------------------------------------------
// iOS only Sass variables can go here
// App Material Design Variables
// --------------------------------------------------
// Material Design only Sass variables can go here
// App Windows Variables
// --------------------------------------------------
// Windows only Sass variables can go here
// App Theme
// --------------------------------------------------
// Ionic apps can have different themes applied, which can
// then be future customized. This import comes last
// so that the above variables are used and Ionic's
// default are overridden.
#import "ionic.theme.default";
// Ionicons
// --------------------------------------------------
// The premium icon font for Ionic. For more info, please see:
// http://ionicframework.com/docs/v2/ionicons/
#import "ionic.ionicons";
// Fonts
// --------------------------------------------------
#import "roboto";
#import "noto-sans";
in order to give background color to swidemenu you have to open app.css and copy paste the below code. basically ion-menu has a content block just like pages. so you have to change the background color of that page( sidemenu )
ion-menu{
ion-content{
background-color:red !important;
}
}
<ion-toolbar yourcolorname>
If you declare a color in the scss like you did with color1, color2 etc. And then you can use your colors as in the example above.
You can create div as header following <ion-list> within <ion-content>
. then apply style to the header and accordingly to the <ion-list>as well.
<ion-menu [content]="content">
<ion-content>
<!--Menu Header-->
<div>
........
</div>
<!--Menu list-->
<ion-list>
</ion-list>
</ion-content>
</ion-menu>
then have ion-navbar in each page you need toolbar.
<ion-header no-border>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
</ion-navbar>
</ion-header>
Now, your next requirement is to have nested menus
// Menu model
export interface MenuOptionModel {
iconName: string;
displayName: string;
component: any;
subItems?: Array<MenuOptionModel>;
}
export class MyApp {
showSubmenu: boolean = false;
options: any[];
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
this.initializeApp(platform, statusBar, splashScreen);
this.getMenuOptions();
}
getMenuOptions() {
this.options = new Array<MenuOptionModel>()
// Load simple menu options
// ------------------------------------------
this.options.push({
iconName: 'icon_home',
displayName: 'HOME',
component: HomePage
});
this.options.push({
iconName: 'icon_settings',
displayName: 'Expenses',
component: null,
subItems: [
{
displayName: 'Exp 1',
component: Exp1Page
},
{
displayName: 'Exp 2',
component: Exp2Page
}
]
});
}
menuItemHandler() {
this.showSubmenu = !this.showSubmenu;
}
Menu Template Page
<ion-list>
<div *ngFor="let option of options">
<button menuClose ion-item *ngIf="!option.subItems" class="menu-style" (click)="openPage(option)">
<ion-label text-wrap mode="md" >
<ion-icon [name]="option.iconName" aria-hidden="true" role="presentation" item-left></ion-icon>
{{ option.displayName }}
</ion-label>
</button>
<div *ngIf="option.subItems && option.subItems.length > 0" class="div-sub-item">
<button ion-item (click)="menuItemHandler()">
<ion-label text-wrap mode="md">
<ion-icon [name]="option.iconName" aria-hidden="true" role="presentation" item-left></ion-icon>
{{ option.displayName }}
</ion-label>
<ion-icon name="icon_down" aria-hidden="true" role="presentation" item-right></ion-icon>
</button>
<div class="settings-menu-divider">
</div>
<ion-list mode="md" >
<div *ngFor="let item of option.subItems" id="side-nav" mode="md">
<button menuClose ion-item submenu-item class="submenu-style" *ngIf="showSubmenu" (click)="openPage(item)"
mode="md">
<ion-label text-wrap mode="md">
<ion-icon name="icon_bullet" aria-hidden="true" role="presentation"
item-left ></ion-icon>
{{ item.displayName }}
</ion-label>
</button>
<div *ngIf="showSubmenu" class="sub-menu-divider"></div>
</div>
</ion-list>
</div>
</div>
</ion-list>
I am having a little challenge trying to re-position an icon in an ion-card header to the right. Check my code samples and pictures below.
this is my code
...
<ion-card *ngFor="let account of Accounts">
<ion-card-header>
<span item-left>{{account.title}}</span>
<span item-right><ion-icon (click)="takeAction(account.$key)" name="more"></ion-icon></span>
</ion-card-header>
<ion-card-content>
<h5>Bank: <b>{{account.bank}}</b></h5>
<h5>Ac name: <b>{{account.acname}}</b></h5>
<h5>Ac name: <b>{{account.acno}}</b></h5>
</ion-card-content>
but the picture below shows what i would love to achieve
I finally figured out the ultimate solution. It is just adding style="float:right;"
...
<ion-card *ngFor="let account of Accounts">
<ion-card-header>
<span style="color:blue">{{account.title}}</span>
<ion-icon style="float: right;" (click)="takeAction(account.$key)" name="more">
</ion-icon>
</ion-card-header>
<ion-card-content>
<h5>Bank: <b>{{account.bank}}</b></h5>
<h5>Ac name: <b>{{account.acname}}</b></h5>
<h5>Ac name: <b>{{account.acno}}</b></h5>
</ion-card-content>
Notice how I placed the ion-header contents into an ion-item element
You could try using ion-row and ion-col with col-{width}.
<ion-card-header>
<ion-row justify-content-between>
<ion-col col-11>
<span item-left>{{account.title}}</span>
</ion-col>
<ion-col col-1>
<span item-right><ion-icon (click)="takeAction(account.$key)" name="more"></ion-icon></span>
</ion-col>
</ion-card-header>
They are both components of ion-grid which works similar to the grid layout of bootstrap.
Use a div container and pull that div to right. HTML should look like below:
<ion-card *ngFor="let account of Accounts">
<ion-card-header>
<span item-left>{{account.title}}</span>
<span item-right>
<div class="pull-right">
<ion-icon (click)="takeAction(account.$key)" name="more"></ion-icon>
</div>
</span>
</ion-card-header>
<ion-card-content>
<h5>Bank: <b>{{account.bank}}</b></h5>
<h5>Ac name: <b>{{account.acname}}</b></h5>
<h5>Ac name: <b>{{account.acno}}</b></h5>
</ion-card-content>
In CSS add following:
.pull-right {
float: right;
}
You could also use the popular flexbox module. Just add the following 2 CSS properties to the parent element.
<ion-card-header>
<div style="display: flex; justify-content: space-between;">
<h1>Left Content</h1>
<ion-icon name="more"></ion-icon>
</div>
...
</ion-card-header>
Here you can find another tutorial about flexbox with a good explanation and some nice, visual appealing examples.
The snippet above results in something like this:
<ion-card>
<ion-card-header>
<ion-card-subtitle color="primary">Subtitle</ion-card-subtitle>
<ion-card-title>
Title
<ion-icon color="primary" style="float:right" name="arrow-back"></ion-icon>
</ion-card-title>
</ion-card-header>
<ion-card-content>
This is some card content
</ion-card-content>
</ion-card>
I want to disable buttons when fields are empty.
Template:
<ion-content padding text-center class="product">
<ion-item>
<ion-input placeholder="Product" type="text" [(ngModel)]="product.productName"></ion-input>
</ion-item>
<ion-item>
<ion-input placeholder="Price" type="number" [(ngModel)]="product.price"></ion-input>
</ion-item>
<button ion-button block (click)="checkPrice()" [disabled]="!(product.price && product.productName) ? true : null">Сравнить цену</button>
<button ion-button block (click)="addToReceipt()" [disabled]="!(product.price && product.productName) ? true : null">Добавить в чек</button>
When I run this on browser - data binding works fine.
when I run this on device - data binding does not work:
I think you can do this using <form> and required in <ion-input> like:
<ion-content padding text-center class="product">
<form #demoForm="ngForm">
<ion-item>
<ion-input placeholder="Product" type="text" [(ngModel)]="product.productName" required></ion-input>
</ion-item>
<ion-item>
<ion-input placeholder="Price" type="number" [(ngModel)]="product.price" required></ion-input>
</ion-item>
</form>
<button ion-button block (click)="checkPrice()" [disabled]="!demoForm.form.valid">Сравнить цену</button>
<button ion-button block (click)="addToReceipt()" [disabled]="!demoForm.form.valid">Добавить в чек</button>
Just an example. You need to check other parts/conditions in your code if you have any.