Okay, I have three relevant files for my question/s..
App.js - app.config
In my app.config
passing value in the resolve
using $stateParams
is possible and I attain it. But how about the value is from the controller
? I need the provCode
value to use it in my php select query
.
.state("editStudent", {
abstract: true,
url:"/student/:id",
controller: "EditStudentController",
templateUrl: "views/students/editStudent.html",
resolve: {
dataservice: 'dataservice',
student: function(dataservice, $stateParams){
var data ={
id : $stateParams.id
};
return dataservice.getStudent(data);
},
provinceData: function(dataservice){
return dataservice.getProvince();
},
municipalityData: function(dataservice){
var provCode = {
provCode : '0456' // <--------------------- this one
}
return dataservice.getCity(provCode);
}
}
})
EditStudentController.js - app.controller
In my app.controller
, I use the angular.forEach
to find province
code
or id
to use it php select query
. As you can see I tried to create function app.getCity
but it is not working as I expected.
console.log(provinceData);
$scope.provinceData = provinceData;
var retProv = $scope.studentInfo.province;
angular.forEach(provinceData, function(value, key) {
if(value.provDesc == retProv){
$scope.studentInfo.province = provinceData[key];
$scope.provCode = provinceData[key].provCode;
console.log($scope.provCode);
}
}); // angular.forEach
var provCode = $scope.provCode;
// Test if it is working but sadly, it is not
// $scope.getCity = function(){
// dataservice.getCity(provCode);
// }
Dataservice.js - app.factory
In my app.factory
there are currently 3 functions. The other 2 functions works expectedly but the factory.getCity()
is my problem right now. As you can see there is a parameter provCode
, this parameter is from my app.config
state
resolve
and it has been assigned a hardcoded/static value.
factory.getStudent = function(data){
return $http.post("php/students/viewOneStudent.php",data).then(function(response){
student = response.data[0];
console.log(student);
return student;
})
}
factory.getProvince = function(){
return $http.get('php/address/province.php').then(function(response){
provinceData = response.data;
console.log(provinceData);
return provinceData;
})
}
factory.getCity = function(provCode){
return $http.post('php/address/cities.php', provCode).then(function(response){
municipalityData = response.data;
console.log(municipalityData);
return municipalityData;
})
}
My Question is:
- If it is possible to pass a value from
controller
to aresolve
? - If it is possible, how can I do that? because I tried my very best in trial and error approach.
- If it is not possible, what is my option/s to achieve what I want?
Note
I tried to do it without resolve
, getting data and posting again for php querying
, it is also works BUT the problem is sometimes data doesn't load when the view
is loaded. As far as I know, read and understand from different sources in the internet, there is a time that view
is loaded but the "work on processing the data" is not finished yet..
I am pretty new and still learning AngularJs. When I am practicing and learning AngularJs coding, I love how it works especially the routing
, I am impressed with it. So, I am trying my very best to understand AngularJs and its complexity.
UPDATE I tried different approach of how I can get my expected results. But I can't get to it, I encountered an error. This is the link of my other approach.