I have the following model:
var MODEL = { myTable: [], anotherTable: [] }; //--- initially set as empty arrays
MODEL
is a global variable as it is used to bind the app model in different controllers.
I have devined a service that updates the model, getting the data from a php page that returns the table rows:
myApp.factory('myAppUtils', function($http) {
return {
updateModelTable: function(tableName) {
var pageToCall = "";
var params = "";
switch(tableName){
case 'myTable':
pageToCall = "myTable.php";
params = "";
break;
case 'anotherTable':
break;
default:
break;
}
var updateModel = function(tableName, data){
MODEL[tableName] = data;
};
var url = "/" + pageToCall;
$http.get(url).
success(function(data, status, headers, config) {
updateModel(tableName, data);
}).
error(function(data, status, headers, config) {
alert("error");
});
}
};
});
and then one of the controllers:
myApp.controller("MyTableCtrl", function($scope, myAppUtils){
$scope.table = MODEL.myTable;
myAppUtils.updateModelTable("playersTable");
});
once I run the app, the myAppUtils.updateModelTable()
function is correctly invoked, it does update successfully the MODEL
but the view controlled by MyTableCtrl
does not update accordingly.
What I'm trying to do is to set up some sort of CRUD operations, getting and setting values from a sql db. I'd like to, for example, insert a new record and then call the update function to update the model and consequently the view, but this is not happening.
Is this the correct "angular" way to do the thing?
Any hint? What am I doing wrong?
Thanks in advance