_co.userData is undefined ionic 3 - angularjs

I am getting Error for the ngModel.
_co.userData is not defined.
I have posted my home.ts and home.html file.
browser console showing the error is near [(ngModel)].
This is my home.ts file
import { Component, NgModule } from '#angular/core';
import { NavController } from 'ionic-angular';
import { AuthService} from '../../providers/auth-service/auth-service';
import { CompanygroupsPage } from '../companygroups/companygroups';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
responseData:any;
userData:{"username":"hi","password":"123"}
constructor(public navCtrl: NavController, public authService:AuthService){}
login(){
console.log(this.userData);
this.navCtrl.push(CompanygroupsPage);
}
}
This is my home.html file
<ion-content padding class="home">
<ion-list class="login">
<form>
<ion-item class="inputs">
<ion-label stacked>Username</ion-label>
<ion-input value="" type="text" [(ngModel)]="userData.username" name="username"></ion-input>
<ion-icon name="contact" item-left></ion-icon>
</ion-item>
<ion-item class="inputs">
<ion-label stacked>Password</ion-label>
<ion-input type="password" value="" [(ngModel)]="userData.password" name="password"></ion-input>
<ion-icon name="lock" item-left></ion-icon>
</ion-item>
<button (click)="login()" class="btn_login" ion-button color="light" round>Login</button>
</form>
</ion-list>
<ion-footer class="footer">
<ion-toolbar>
...
</ion-toolbar>
</ion-footer>
Error is near
<ion-input value="admin" type="text" [(ngModel)]="username" name="username"></ion-input>

You need to assign,
Change
From
userData:{"username":"hi","password":"123"}
To
userData = {"username":"hi","password":"123"}

Related

ERROR Error: Cannot find control with name in ionic3?

I have create a app using ionic 3 and i click on the login link i got this error "Cannot find control with fname, email,number,gstNumber and i have tried formGroupName instead of the formControlName but still not working Please help me for figuring out what I am doing wrong and i have include the page of login and createAccount? `
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { CreateAccountPage } from '../create-account/create-account';
import { LoginPage } from '../login/login';
#Component({
selector : 'page-home',
templateUrl : 'home.html'
})
export class HomePage {
constructor(public navCtrl : NavController) {
}
openLoginPage() {
this.navCtrl.setRoot(LoginPage);
}
openCreateAccount(){
this.navCtrl.setRoot(CreateAccountPage);
}
}
<ion-content class="masters" >
<ion-card>
<img src="assets/imgs/riseBgdd.jpg" height="550px">
<button class="card-button button-color-yell" color="light" outline small ion-button style="left:90px" (click)="openLoginPage()" >
Login
</button>
<button class="card-button1 button-color-blue" color="light" outline small ion-button style="left:150px" (click)="openCreateAccount()" >
Create Account
</button>
</ion-card>
</ion-content>
//This is our Creat account page
<!DOCTYPE html>
<ion-content>
<h3 text-center>
Create Account
</h3>
<form [formGroup]="createAccountForm" >
<ion-list>
<ion-item class="round-inp centerIt">
<ion-input formControlName="firmName" type="text" placeholder="Firm name" >
</ion-input>
</ion-item>
<br>
<ion-item class="round-inp centerIt">
<ion-input formControlName="yourName" type="text" placeholder="Your name" [(ngModel)]="yourName">
</ion-input>
</ion-item>
<br>
<ion-item class="round-inp centerIt">
<ion-input formControlName="email" type="email" placeholder="Email" [(ngModel)]="email">
</ion-input>
</ion-item>
<br>
<ion-item class="round-inp centerIt">
<ion-input formControlName="mobileNumber" type="number" placeholder="Mobile number" [(ngModel]="mobileNumber">
</ion-input>
</ion-item>
<br>
<ion-item class="round-inp centerIt">
<ion-input formControlName="gstNumber" type="number" placeholder="GST number" [(ngModel)]="firmName">
</ion-input>
</ion-item>
</ion-list>
</form>
</ion-content>
<!DOCTYPE html>
<ion-content class="masters">
<br>
<div class="centerIt">
<ion-img src="assets/imgs/icon image.jpg"
width="60" height="60"
class="app-icon">
</ion-img>
</div>
//This is our login page
<form [formGroup]="loginForm" (submit)="loginUser($event)">
<ion-list>
<ion-item class="round-inp centerIt">
<ion-label>
Email or Phone
</ion-label>
<ion-input formControlName="email" type="text" value=""></ion-input>
</ion-item >
<br>
<ion-item class="round-inp centerIt">
<ion-label>
Password
</ion-label>
<ion-input formControlName="password" type="password"></ion-input>
</ion-item>
<br>
<br>
<div class="centerIt">
<button ion-button outline
class="button-color-yellow"
(click)="openSelectCategory()">
Login
</button>
<button ion-button outline
class="button-color-yello"
(click)="openCreateAccount()">
Create Account
</button>
</div>
<br>
<br>
<div class="centerIt">
<a href="openLogin()" class="link-align-color" >
Forgot Password
</a>
</div>
</ion-list>
</form>
</ion-content>
`
You can change your object from any to FormGroup type:
private createAccountForm : FormGroup
I'm pretty sure, that you don't need ngModel. Try also the other method of binding:
formControlName
<ion-item>
<ion-label floating>Email</ion-label>
<ion-input formControlName="email" type="email"></ion-input>
</ion-item>
[formContol]
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input [formControl]="createAccountForm.controls['password']" type="text"></ion-input>
</ion-item>
I made similar example on plunkr with forms and it just works fine, check it out:
https://plnkr.co/edit/zGenYZz5lzGbWBBTBS9P

How to apply jquery plugin: styler in typescript

I want to apply jQueryFormStyler in Angular2/Typescript.
I call in ngAfterViewInit:
jQuery(this.elementRef.nativeElement).find('input').styler();
My typescript component:
import {Component, AfterViewInit, ElementRef} from "#angular/core";
declare var jQuery: any;
#Component({
selector: 'my-app',
template: ` <div class="row">
<div class="col-sm-12 item">
<div class="row">
<div class="col-sm-6 vcenter">
<div class="position">
Java Developer
</div>
<div class="place">
Company name
</div>
</div>
<div class="col-sm-6 vcenter text-right">
<label class="radio-wrapper">
<input type="radio" name="choose-role4">
<span>Viewer</span>
</label>
<label class="radio-wrapper checked">
<input type="radio" name="choose-role4" checked="">
<span>Moderator</span>
</label>
<label class="radio-wrapper">
<input type="radio" name="choose-role4">
<span>HIDE OBJECT</span>
</label>
</div>
</div>
</div>
</div>`
})
export class AppComponent implements AfterViewInit {
elementRef: ElementRef;
constructor(elementRef: ElementRef) {
this.elementRef = elementRef;
}
ngAfterViewInit() {
**jQuery(this.elementRef.nativeElement).find('input').styler();**
}
}
Thank you.

Bootstrap Modal with Angular 2

I am working on a modal with Angular 2. So far so good no errors but then... When i press on the button nothing appears. Am i missing something? Surely i do..
this is my template
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
{{demoInfo | json}}
</div>
<div class="modal-body">
<form class="form-horizontal" id='myForm' role="form" [ngFormModel]="CreateGroup">
<div class="col-md-7">
Name: <input type="text" [(ngModel)]='demoInfo.name' class="form-control" ngControl='name'>
</div>
<div class="col-md-7">
Password: <input type="password" [(ngModel)]='demoInfo.password' class="form-control" ngControl='password'>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" [disabled]='!CreateGroup.valid' (click)="addNewGroup(demoInfo)" class="btn btn-primary">Create</button>
</div>
</div>
</div>
</div>
this is my template component
import { Component } from '#angular/core';
import { ROUTER_DIRECTIVES } from '#angular/router';
import { CORE_DIRECTIVES, NgClass, FORM_DIRECTIVES, Control, ControlGroup, FormBuilder, Validators } from '#angular/common';
class DemoInfo {
name: string;
password: string;
}
#Component({
template: require('./template.component.html'),
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES]
})
export class ModalComponent {
CreateGroup: ControlGroup;
demoInfo: DemoInfo;
constructor(fb: FormBuilder) {
this.demoInfo = new DemoInfo();
this.CreateGroup = fb.group({
'name': new Control(this.demoInfo.password),
'password': new Control(this.demoInfo.password)
})
}
addNewGroup(demoInfo: DemoInfo) {
console.log(demoInfo, 'whole object');
this.demoInfo = new DemoInfo();
}
}
Make sure have installed Bootstrap.js. Angular-ui won't work without it.
Make sure in your script tag the Bootstrap installs first.
src="~/Scripts/bootstrap.js"
src="~/Scripts/angular.js"
src="~/Scripts/app/app.js"
src="~/Scripts/ui-bootstrap-tpls-2.0.0.js"
Make sure you add a dependency to ui.bootstrap in your module (at least in Angular 1.5) I'm unfamiliar with Angular 2.
Let me know if this doesn't fix your issue.
I didn't add ng2-bootstrap library in my Angular js.
If you are new with Angular and you want to implement bootstrap js components the correct way i recommend to read the ng2 documentation.
I followed the steps on this link https://valor-software.com/ng2-bootstrap/#/

My modal dialog as a funciton in ts file

Hello my fellow stackoverflowers,
I guess i have a simple question.
In my html i have a Modal Dialog that pops up. It uses ng2 Bootstrap. It works and all but... I want this code
"<button class="btn btn-primary" (click)="showModal()">Login</button>"
(showModal())
replace it in my ts file as a function
showModal(modal: ModalDirective) {
}
How can i do this? i am already struggling more then a hour.
This is my full HTML Code.
<button class="btn btn-primary" (click)="showModal()">Login</button>
<!-- Modal -->
<div bsModal #lgModal="bs-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" (click)="lgModal.hide()" aria-label="Close">
<span aria-hidden="true"> ×</span>
</button>
<h4 class="modal-title">Login</h4>
</div>
<div class="modal-body">
<form id="myForm" role="form" [ngFormModel]="CreateGroup">
<div class="form-group">
<div class="input-group">
<input type="text" [(ngModel)]='demoInfo.name' class="form-control" ngControl='name' placeholder="Gebruikersnaam">
<label for="uLogin" class="input-group-addon glyphicon glyphicon-user"></label>
</div>
</div>
<div class="form-group">
<div class="input-group">
<input type="password" [(ngModel)]='demoInfo.password' class="form-control" ngControl='password' placeholder="Wachtwoord">
<label for="uPassword" class="input-group-addon glyphicon glyphicon-lock"></label>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" [disabled]='!CreateGroup.valid' (click)="addNewGroup(demoInfo)" class="btn btn-primary">Login</button>
</div>
</div>
</div>
</div>
and hits is my full TS code
import { Component } from '#angular/core';
import { ROUTER_DIRECTIVES } from '#angular/router';
import { CORE_DIRECTIVES, FORM_DIRECTIVES, Control, ControlGroup, FormBuilder } from '#angular/common';
import { ModalDirective, BS_VIEW_PROVIDERS} from 'ng2-bootstrap/ng2-bootstrap';
class DemoInfo {
name: string;
password: string;
}
#Component({
template: require('./template.component.html'),
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES, ModalDirective],
viewProviders: [BS_VIEW_PROVIDERS]
})
export class ModalComponent {
CreateGroup: ControlGroup;
demoInfo: DemoInfo;
modal: ModalDirective;
constructor(fb: FormBuilder) {
this.demoInfo = new DemoInfo();
this.CreateGroup = fb.group({
'name': new Control(this.demoInfo.name),
'password': new Control(this.demoInfo.password)
})
}
addNewGroup(demoInfo: DemoInfo) {
console.log(demoInfo, 'whole object');
this.demoInfo = new DemoInfo();
}
showModal(modal: ModalDirective) {
}
hideModal(modal: ModalDirective) {
}
}
So this is what i did.
I added #ViewChild('lgModal') modal: any; in my ModalComponent
made a new function called showModal()
and referenced it by calling this.modal.dosomething();

Unable to see the form data in console after clicking the button,angular2

I created a simple login form with which i use to see the input data in my console but i am getting an empty object.Can any one help me out where the error is
My template,
<div class="login jumbotron center-block">
<h1>Login</h1>
<form #form ="ngForm" (ngSubmit)="onSubmit(form.value)">
<div class="form-group">
<label for="username">Username</label>
<input type="text" ngControl ="email" class="form-control" id="emailh" placeholder="Username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" ngControl ="phone" class="form-control" id="phoneh" placeholder="Password">
</div>
<button class="btn btn-default">Submit</button>
</form>
<a [routerLink]="['/demo/signup']" >Sign Up</a>
<div class="col-md-9">
<router-outlet></router-outlet>
</div>
</div>
My component,
import { Component } from '#angular/core';
import { Router, ROUTER_DIRECTIVES } from '#angular/router';
import { CORE_DIRECTIVES } from '#angular/common';
import { Http, Headers } from '#angular/http';
import { contentHeaders } from '../headers/headers';
import {FORM_DIRECTIVES,FormBuilder,FormGroup,Validators, REACTIVE_FORM_DIRECTIVES} from '#angular/forms';
#Component({
directives: [CORE_DIRECTIVES,ROUTER_DIRECTIVES,FORM_DIRECTIVES,REACTIVE_FORM_DIRECTIVES],
templateUrl : "./components/login/login.html",
})
export class Login {
constructor(public router: Router, public http: Http) {
}
onSubmit(form:any) {
console.log(form);
}
}
I am using Angular2 rc4 here is my package.json
"dependencies": {
"#angular/common": "2.0.0-rc.4",
"#angular/compiler": "2.0.0-rc.4",
"#angular/core": "2.0.0-rc.4",
"#angular/forms": "0.2.0",
"#angular/http": "2.0.0-rc.4",
"#angular/platform-browser": "2.0.0-rc.4",
"#angular/platform-browser-dynamic": "2.0.0-rc.4",
"#angular/router": "3.0.0-alpha.8",
...........
}
and I updated your code in my project, and it is working like a charm. Update your code like below -
<form [formGroup]="myForm"
(ngSubmit)="onSubmit(myForm.value)"
class="ui form">
<div class="form-group"
[class.error]="!email.valid && email.touched">
<label for="emailInput">Email</label>
<input type="text"
id="emailInput"
placeholder="EMAIL"
[formControl]="email">
</div>
<div>
<!-- Do it for password too !-->
</div>
<div *ngIf="!myForm.valid"
class="ui error message">Form is invalid</div>
<button type="submit" class="ui button">Submit</button>
</form>
Hope , It works for you. Actually it will :)

Resources