I am a beginner working on my first Angular/Ionic project. Basically, I have a form that I ask the user to fill and submit. On clicking the submit button, I want the information entered to be made into a JS object and stored in the API. But, no matter what the user inputs, the object values are showing to be undefined.
Here are the files.
appApi.js:
angular.module('myApp').factory('appApi', function() {
var data = [{
"fName": "Subscription",
"desc": "Guitar Learning Session",
"selfExpiryDate": "3/20/2015",
"selfExpiryTime": "18:15"
}];
var getData = function() {
return data;
}
var addData = function(newData) {
data[data.length] = newData;
}
return {
getFavors: getFavors,
addFavor: addFavor
};
});
controller.js:
.controller('formCtrl', ['$scope', '$state', 'appApi', function($scope, $state, appApi) {
$scope.newData = {
"fName": $scope.fNameForm,
"desc": $scope.descForm,
"selfExpiryDate": $scope.selfExpiryDateForm,
"selfExpiryTime": $scope.selfExpiryTimeForm
};
$scope.submit = function() {
appApi.addData($scope.newData);
$state.go('app.home');
}
}])
and form.html:
<ion-view ng-controller = "formCtrl">
<ion-content>
<div class = "list list-inset">
<label class="item item-input">
<input type="text" ng-model = "fNameForm" placeholder="Favor Name" autofocus>
</label>
<label class="item item-input">
<input type="text" ng-model = "descForm" placeholder="Favor Description">
</label>
<label class="item item-input">
<input type="text" ng-model = "selfExpiryDateForm" placeholder="Favor Expiry Date">
</label>
<label class="item item-input">
<input type="text" ng-model = "selfExpiryTimeForm" placeholder="Expiry Time">
</label>
</div>
<button class="button button-block button-positive" ng-click = "submit()">
Submit
</button>
</ion-content>
</ion-view>
I am not sure if the way I am trying to update data dynamically is even right. Can someone tell?
Also, I want the user to be re-directed to the home page once he submits the form. Is the way I have done it correct? My url changes but the view doesn't.
Since you want to use $scope.newData
in controller, then you should use this object for corresponding ngModel
's:
<div class="list list-inset">
<label class="item item-input">
<input type="text" ng-model="newData.fName" placeholder="Favor Name" autofocus="" />
</label>
<label class="item item-input">
<input type="text" ng-model="newData.desc" placeholder="Favor Description" />
</label>
<label class="item item-input">
<input type="text" ng-model="newData.selfExpiryDate" placeholder="Favor Expiry Date" />
</label>
<label class="item item-input">
<input type="text" ng-model="newData.selfExpiryTime" placeholder="Expiry Time" />
</label>
</div>
At the very least, make sure your capitalization is all matching:
http://plnkr.co/edit/Fvu3hb7R8srRh2ynPrGL?p=info
(function() {
'use strict';
FormCtrl = function($scope, $state, AppApi) {
$scope.submit = function() {
AppApi.addData($scope.newData);
$state.go('app.home');
};
};
AppApi = function() {
var data = [{
"fName": "Subscription",
"desc": "Guitar Learning Session",
"selfExpiryDate": "3/20/2015",
"selfExpiryTime": "18:15"
}];
var getData = function() {
return data;
};
var addData = function(newData) {
data[data.length] = newData;
};
return {
getFavors: getFavors,
addFavor: addFavor
};
};
var myApp = angular.module('myApp');
myApp.factory('AppApi', AppApi);
myApp.controller('FormCtrl', ['$scope', '$state', 'AppApi', FormCtrl]);
})();
Since you have one app, one controller, and one factory-- the way you are loading everything will work-- when you get into more than one controller and factories and whatnot (you are only allowed to define one app, that is so you don't make one of my rookie mistakes)
A larger example you can see working is here: http://plnkr.co/edit/wTIo8y6Det9Y5VLP86pM