I'm very new to AngularJS and am having issues figuring out how to update a $scope element I created from a JSON external data file.
Basically I have a service that contains the function which grabs the JSON:
MyApp.factory("wwprService", function($http){
var _wwpr = [];
var _getwwpr = function(){
$http.get("/js/data/wwpr.json")
.then(function(results){
//Success
angular.copy(results.data, _wwpr);
//alert(results);
}, function(results){
//Error
})
}
var _addNewwwpr = function(val1,val2,val3,val4){
_wwpr.splice(0,0, val1, val2, val3, val4);
$http.get("/js/data/wwpr.json") // here i want to add new datas
}
return{
wwpr: _wwpr,
getwwpr: _getwwpr,
addNewwwpr: _addNewwwpr
};
});
I then have a Controller that contains a function that gets the JSON data on a button click and puts it in $scope.wwp and a second function that I would like to use to update $scope.wwp in that JSON data file:
MyApp.controller("wwprCtrl", function ($scope, wwprService){
// Executes when the controller is created
$scope.wwpr = wwprService.wwpr;
wwprService.getwwpr();
$scope.addNewwwpr = function(wwpProjectName,wwpactivityType,wwpplanDate,wwpefforts){
//alert(wwpProjectName);
var getProjectName = {projectName: wwpProjectName};
var getactivityType = {activityType: wwpactivityType};
var getplanDate = {planDate: wwpplanDate};
var getefforts = {efforts: wwpefforts};
wwprService.addNewwwpr(getProjectName,getactivityType,getplanDate,getefforts);
$scope.wwpr.push({projectName: wwpProjectName});
}
});
JSON data file:
[
{
"projectName": "PMD",
"activityType": "Coding",
"planDate": "12/12/14",
"efforts": "8"
},
{
"projectName": "PMD",
"activityType": "Coding",
"planDate": "12/12/14",
"efforts": "8"
} //here i want add new json data
]
Currently, I successfully get the JSON data from external file and am able to use it to populate aspects of my view, but I am stuck on how to proceed with updating $scope.wwp so that:
- It actually updates
- The update is reflected in my view
I have tried $scope.wwp.push, $scope.wwp.posts.push. These have either flat out not worked or given errors. I'm sure it might be a simple answer, but I feel I may be inexperienced with AngularJS and JSON to pick up on it.