Ionic1 Edit Record & Populate Fields - angularjs

I have an Ionic1 application which has a function which allows the user to edit a record. For example, change a contact from prospect to customer.
When I click edit on the prospect, this loads a view for editing the prospect.
My application then shows a form and populates the fields with the prospect data after a $http.get request allowing the user to amend / update the prospect as required.
My problem, when clicking edit only some of the dropdown lists are populating.
My controller for editing a prospect
.controller('prospectCtrlEdit', function($scope, $http, $state, $stateParams, $ionicLoading, prospects, customers) {
$ionicLoading.show();
$http.get('URL_API_END_POINT/' + $stateParams.customer + '/edit?token=' + localStorage.getItem("token")).then(function(successResponse){
$scope.return = successResponse;
$scope.prospect = $scope.return.data.data.prospect;
$ionicLoading.hide();
}, function(errorRepsonse){
$scope.error = errorRepsonse;
$scope.$broadcast('scroll.refreshComplete');
$ionicLoading.hide();
});
});
My form
<div class="list">
<label class="item item-input item-select">
<div class="input-label">
Type
</div>
<select ng-model="prospect.client_type">
<option value="2">Prospect</option>
<option value="1">Customer</option>
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">
Title
</div>
<select ng-model="prospect.title">
<option value="Mr and Mrs">Mr & Mrs</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>
</select>
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">First Name</span>
<input type="text" placeholder="First name" ng-model="prospect.first_name">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Last Name</span>
<input type="text" placeholder="Last name" ng-model="prospect.last_name">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Email</span>
<input type="email" placeholder="Email address" ng-model="prospect.email">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Mobile</span>
<input type="tel" placeholder="Mobile" ng-model="prospect.mobile">
</label>
<div class="item">
<button class="button button-block" ng-click="updateProspect()">Update Profile</button>
</div>
</div>
For reference, I've included my response from the API which is what is being used to populate the form.
{
"data":{
"status":"success",
"prospect":{
"id":5887,
"user_id":9,
"title":"Mr and Mrs",
"company":null,
"first_name":"Peter",
"last_name":"Prospect",
"email":"",
"mobile":"",
"telephone":null,
"addressline1":null,
"addressline2":null,
"addressline3":null,
"addressline4":null,
"lat":null,
"long":null,
"town":null,
"postcode":null,
"county":null,
"country":null,
"comments":null,
"status":1,
"created_at":"2017-07-12 20:52:48",
"updated_at":"2017-07-12 20:52:48",
"client_type":2,
"prospect_converted":null
}
}
}
To recap. When the edit prospect page loads the form is populated with
all the current data from the above $http.get() request. The user
updates the information as desired - the problem is dropdowns like
"Type" for determining the type either Customer or Prospect isn't auto
populating however the title is.

Related

$scope.watch doesn't work in ionic framework

$scope.watch doesn't work in ionic framework
I was designing some fliter in ionic..It won't work ..the same code is working properly on blank test app as well as angularjs...
My html
<div class="bar bar-header bar-royal">
<div class="h1 title">Hotel search</div>
</div>
<ion-view>
<ion-content>
<h4 style="margin-top:100px;color:#116262;margin-left:10px;font-weight:bold"><i class="icon ion-edit"></i> Request Form</h4>
<div class="list" style="margin-top:15px;">
<label class="item item-input item-select">
<div class="input-label" style="font-weight:bold">
City
</div>
<select ng-model="formData.city">
<option value="Chennai">Chennai</option>
<option value="Delhi">Delhi</option>
<option value="Mumbai">Mumbai</option>
<option value="Goa">Goa</option>
</select>
</label>
<label class="item item-input item-stacked-label">
<span class="input-label" style="font-weight:bold">CheckIn</span>
<input type="date" ng-model="formData.checkIn" value="{{formData.checkIn | date:'yyyy-MM-dd'}}">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label" style="font-weight:bold">checkOut</span>
<input type="date" ng-model="formData.checkOut" value="{{formData.checkOut | date:'yyyy-MM-dd'}}">
</label>
<label class="item item-input item-select">
<div class="input-label" style="font-weight:bold">
Rooms
</div>
<select ng-model="formData.room">
<option value="1" ng-selected="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label>
<label class="item item-input item-select">
<div class="input-label" style="font-weight:bold">
Adults
</div>
<select ng-model="formData.adult">
<option value="1" ng-selected="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label>
<div class="row center" style="margin-top:20px;">
<button class="button button-outline button-block button-royal" ng-click="submit(formData)" style="text-align:center;">
Search
</button>
</div>
{{formData}}
<input type="text" ng-model="useStars"/>
{{fooChanges}}
</div>
</ion-content>
</ion-view>
My controller is
.controller('PlaylistsCtrl', function($scope,$state) {
$scope.formData ={};
$scope.formData.checkIn = new Date();
$scope.formData.checkOut= new Date();
$scope.formData.checkOut.setDate($scope.formData.checkOut.getDate() + 1);
$scope.formData.room = 1;
$scope.formData.city = 'Chennai';
$scope.formData.adult = 1;
$scope.useStars = 'balakumaran';
$scope.fooChanges = 1;
$scope.$watch(function () {
return {
useStars : $scope.useStars,
}
},function (value) {
alert('');
},true);
$scope.submit = function(formData){
$state.go('app.search',{formData:formData});
}
})
what's problem on above code ?? ..I tried lot of times..it won't wrk..if have any alternative idea's??
As said in comment the problem was resolved doing the following :
<input type="text" ng-model="$parent.useStars"/>
However the best way to never have this problem : always use an intermdediary object like $scope.data.userStarts. So you won't ever need the $parent trick, this is called dot notation in Angular and is due to limit of javascript inheritance.
In your button you are passing the formData as parameter
<button class="button button-outline button-block button-royal" ng-click="submit(formData)" style="text-align:center;">
Search
</button>
in your controller there is not formData parameter is refereed except in your search button in the same way you should pass the useStars .
.controller('PlaylistsCtrl', function($scope,$state) {
formData ={};
formData.checkIn = new Date();
formData.checkOut= new Date();
formData.checkOut.setDate($scope.formData.checkOut.getDate() + 1);
formData.room = 1;
formData.city = 'Chennai';
formData.adult = 1;
$scope.useStars = 'balakumaran';
$scope.fooChanges = 1;
$scope.$watch(function () {
return {
useStars : $scope.useStars,
$state.go('app.search',{formData:formData});
}
},function (value) {
alert('');
},true);
$scope.submit = function(formData){
$state.go('app.search',{formData:formData});
}
});
Here you don't need $scope before form angularjs itself create a scope variable if some event occurs.
but there is no event is occurring so there is no way for PlaylistCtrl can understand $scope.formData so try to make an event to be appeare or try to understand the $scope.
so that will help for two way databinding

write to json file possible?

I have a html file as follows to enter details of user, when i press submit it should go to a file created my myself(myfile.json)which is empty now.
please provide code for that.
<ion-view view-title="employee">
<ion-content>
<!-- <input type="date" id=FromDate" name="FromDate" ng-model="FromDate" date-format="yyyy-MM-dd"/> -->
<form name="formname" ng-submit="logdata(formname)">
<h1 >{{data.FromDate|date}}</h1>
<label class="item item-input ">
<div class="input-label">date:</div>
<input type="date" name="name" value="" ng-model="data.FromDate"></input>
</label>
<div class="list">
</div>
<div class="list">
<label class="item item-input item-select">
<div class="input-label">
Project
</div>
<select ng-options="project.name as project.name for project in projects" ng-model="data.selectedProject">
<!-- <option>createwhimsy</option>
<option selected>activity tracker</option>
<option>big iron</option> -->
</select>
</label>
</div>
<div class="list">
<label class="item item-input item-select">
<div class="input-label">
Manager
</div>
<select ng-model="data.manager">
<option selected>deepa</option>
</select>
</label>
</div>
<label class="item item-input item-stacked-label">
<span class="input-label">task</span>
<textarea placeholder="task details goes here" rows="8" cols="10" ng-model="data.task"></textarea>
</label>
<div class="list">
<label class="item item-input item-select">
<div class="input-label">
Estimated time(hrs)
</div>
<select ng-model="data.time">
<option selected>1</option>
<option selected>2</option>
<option selected>3</option>
<option selected>4</option>
<option selected>5</option>
<option selected>6</option>
</select>
</label>
</div>
<button type="submit" class="button button-calm">
submit
</button>
</form>
</ion-content>
</ion-view>
possible, need simple code, in controller.with pure angularjs.

How to validate a combo box in ionic framework

I am creating a mobile app using ionic framework.
I have a form which I have created for my hybrid mobile application..
I need to check whether the user has filled all the fields in the form..
my code...
<ion-view view-title="Request">
<ion-content>
<form novalidate>
<div class="list">
<label class="item item-input item-select">
<div class="input-label">
Request Type:
</div>
<select>
<option selected>--Please select--</option>
<option>Car Pass Ticket</option>
<option>Seminar Pass</option>
<option>Identy Card</option>
</select>
</label>
<label class="item item-input">
<textarea placeholder="Description" name="description" ng-minlength="20" required ></textarea>
</label>
<br/>
<!-- <div class="padding">
<button class="button button-positive" ng-click="submit(description)">
Submit
</button>
</div> -->
<div class="padding">
<button class="button button-positive" ng-disabled="request.$invalid" ng-click="submit(description)">
Submit
</button>
</div>
</div>
</form>
</ion-content>
</ion-view>
Can some one kindly help me to validate the combo-box..
any kind of help is highly appreciated......
This should work
<form name="register_form" ng-submit="submitDetails(user)" novalidate="">
<div class="list">
<label class="item item-input item-floating-label" style="position:relative;">
<span class="input-label">First Name</span>
<input type="text" name="user_first_name" placeholder="First Name" ng-model="user.firstName" ng-required="true">
<p ng-show="register_form.user_first_name.$invalid && !register_form.user_first_name.$pristine" class="help-block">You name is required.</p>
</label>
<!--omitted-->
<input type="submit" class="button button-royal" value="register">
</div>
</form>
Form name is register_form,
<form name="register_form" ng-submit="submitDetails(user)" novalidate="">
Input name is user_first_name,
<input type="text" name="user_first_name" placeholder="First Name" ng-model="user.firstName" ng-required="true">
So validation must pass through those fields
<p ng-show="register_form.user_first_name.$invalid && !register_form.user_first_name.$pristine" class="help-block">You name is required.</p>
Model itself doesn't have $invalid or $pristine properties, so it doesn't make sense
For phone field
<input type="number" name="user_phone" placeholder="Phone No" ng-model="user.phone" ng-minlength="10" ng-maxlength="10" ng-required="true">
<span class="help-block" ng-show="register_form.user_phone.$error.required || register_form.user_phone.$error.number">Valid phone number is required</span>
<span class="help-block" ng-show="((register_form.user_phone.$error.minlength || register_form.user_phone.$error.maxlength) && register_form.user_phone.$dirty) ">phone number should be 10 digits</span>
Try this:
1) Give name attribute to your form
<form name="myForm" novalidate>
2) declare the request types inside of your scope like this:
$scope.requestType = [
{ code: "carPass", name: "Car Pass Ticket" },
{ code: "seminarPass", name: "Seminar Pass" },
{ code: "identityCard", name: "Identy Card"}
];
3) declare select box like this:
<select name="requestType" ng-model="request" required
ng-options="request.code as request.name for request in requestType" >
<option value="">--Please select--</option>
</select>
4) Inside submit method check for $valid attribute of form.
$scope.submit1 = function(description){
if($scope.myForm.$valid){
// Do your stuff
}else{
// Do your stuff
}
}

angular isn't respecting all my required fields

In my page below everything is working except for the user.productNumber input at the bottom. What I mean is that all the "required" fields are keeping the "Create Account" button disabled but it's like the productNumber input is being ignored. How can I get this working?
<ion-view title="Sign Up">
<ion-content>
<form name="signUpForm">
<div class="list">
<label class="item item-input item-stacked-label">
<input data-ng-model="user.email" data-ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/" type="email" placeholder="user#mail.net" required>
</label>
<label class="item item-input item-stacked-label">
<input data-ng-model="user.password" type="password" placeholder="password" required>
</label>
<label class="item item-input item-stacked-label">
<input data-ng-model="user.passwordAgain" type="password" placeholder="password (again)" required>
</label>
<li class="item item-checkbox">
<label class="checkbox">
<input data-ng-model="user.newsletter" type="checkbox" checked>
</label>
Weekly Newsletter
</li>
<label class="item item-input item-stacked-label">
<input data-ng-modal="user.productNumber" data-ng-pattern="/^[a-zA-Z0-9-]{8}$/" type="text" placeholder="Product Number" required>
</label>
<button data-ng-click="createAccountClick()" data-ng-disabled="signUpForm.$invalid" class="button button-full button-positive">
Create Account
</button>
</div>
</form>
</ion-content>
</ion-view>
This is because you mispelled data-ng-model with data-ng-modal.
Simply change:
data-ng-modal="user.productNumber"
with
data-ng-model="user.productNumber"

Angularjs input field is getting stuck on typing in google chrome

I have 4 fields in a form firstname,lastname,email,phone
when i start typing on email and phone field the form gets stuck in Google chrome browser(keyboard typing and mouse click on form section stops working but outside form everything eg:tabs are working), but when i try with Firefox there is no problem.
I tried in Google chrome by removing firstname field now the error occurs only in phone field. likewise when i remove lastname field there was no error.
In general only the first 2 fields are working 3rd field onwards its showing error.
How can i sole this issue.
.controller('AccountCtrl', function($scope) {
$scope.usr = {"firstname":"","lastname":"","umail":"","uphone":"","location":["",""]};
if(localStorage.getItem('appusr')!==null){
$scope.usr = JSON.parse(localStorage.getItem('appusr'));
console.log($scope.usr);
}
$scope.saveUserDetails = function(){
if($scope.usr.location[0]==""){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
$scope.$apply(function(){
$scope.usr.location = [position.coords.latitude,position.coords.longitude];
});
});
}
}
console.log($scope.usr);
localStorage.setItem("appusr",JSON.stringify($scope.usr));
}
});
<form ng-submit="saveUserDetails()" name="form" validate>
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">First Name</span>
<input type="text" ng-model="usr.firstname" placeholder="John">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Last Name</span>
<input type="text" ng-model="usr.lastname" placeholder="Suhr">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Email</span>{{usr.email}}
<input type="email" ng-model="usr.umail" name="umail" placeholder="john#suhr.com">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Phone</span>
<input type="text" ng-model="usr.uphone" placeholder="9999999999">
</label>
<button type="submit" class="button button-full button-positive"><i class="icon ion-person-add"></i> Update </button>
</div>
</form>

Resources