I have a view with a form that asks a user for some details eg id, name and surname. To that effect, I have a person object created in a service as below to save the users information.
var personObject = angular.module("personObject", []);
personObject.service("PersonObject", function () {
var Person = {
id: "",
name: "",
surname: ""
};
return {
getPerson: function () {
return Person;
}
};
});
When the user completes the form the values for id , name and surname are set in the Person . This is working as intended and I can view the details back using get() in the views controller.
I then pass the person object to another service that should insert the user details in a SQLite database table. From my views controller I save the details to Database in my $scope.save function.
var formDetails = angular.module("formDetailsController", []);
formDetails.controller("FormDetailsController", function ($scope, UpdatePerson, PersonObject) {
var init = function () {
document.addEventListener("deviceready", onDeviceReady, false);
};
init();
function onDeviceReady() {
};
// Save details to database
$scope.save = function () {
var update = UpdatePerson.update(PersonObject.getPerson()); // Get the person object with user defined values and pass to UpdatePerson service
update.then(
function (success) {
// ToDo
}, function (fail) {
alert(fail.message);
});
};
});
My UpdatePerson service should insert the person object values to a SQLite table. The table is working as intended and have been tested with some hard coded values. My update() function looks as follows (full code given). I am unable to access the person object values however.
var update = angular.module("updatePerson", []);
update.service("UpdatePerson", function ($q, Database) {
// Initialise variables
var db = Database.init(); // Working
var updated = "";
var myFunctions = {
update: function (MyPerson) {
alert("MyPerson + // Returns [object Object]
"\n" + MyPerson.id + // Returns nothing
"\n" + MyPerson.name + // Returns nothing
"\n" + MyPerson.surname // Returns nothing);
var deferred = $q.defer();
var sql = 'INSERT OR IGNORE INTO tb_person (id, name, surname) VALUES (' + MyPerson.id + ', "' + MyPerson.name + '", "' + MyPerson.surname + '")';
var success = function (tx, results) {
deferred.resolve({ updated: true });
}
var error = function (tx, err) {
deferred.reject({
updated: false, message: 'An error has occured});
}
db.transaction(function (tx) {
tx.executeSql(sql, [], success, error);
});
return deferred.promise;
},
}
return myFunctions;
});
I am unable to access the values for my Person object in my update() function as above to insert into the table. It return a blank (not NULL or undefined) - just blank. How do I access the objects values?
I think you are missing declarations
.factory('Service1',['Service2', function (service2,$q, $timeout) {
var sendDataToOtherService= function (data) {
service2.updateData(data[0]);
return true;
}
return {
sendDataToOtherService:sendDataToOtherService
};
}])
.factory('Service2', function ($q, $timeout) {
var updateData= function (data) {
alert(data.name);
return true;
}
return {
updateData:updateData
};
});
Have a look at demo Link http://plnkr.co/edit/oz7BA6rTW8vYuIHOYBxL?p=preview
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.