I am still learning angularjs and I have problem with understanding difference between $scope
and model
object and this currently block me to organize (use some best practice) my app.
As I understand $scope
should be read only (watched some tutorials where I heard this).
So when I load app I should use service
to get some data from database and store it in model
.
UPDATE
Right now all data that I get from server are stored in controller $scope and I am trying to move it to services and make controller dumber.
I also check this article and I am trying to use second or third option but still can't find best way to implement it.
This is my service and controller:
function dataService($http) {
var service = {
getToDoList: getToDoList,
getToDoListByType: getToDoListByType,
getToDoById: getToDoById
};
return service;
function getToDoList() { }
function getToDoListByType() { }
function getToDoById() { }
}
function toDoController($location) {
var vm = this;
vm.todos = [];
vm.title = 'toDoController';
activate();
function activate() {
return getToDos().then(function () {
console.log("ToDos loaded");
});
}
function getToDos() {
return dataservice.getToDoList()
.then(function (data) {
vm.todos = data;
return vm.todos;
});
}
}
But in this implementation to do list is again in the controller.
Where should I store this list after I get it from server and from where it should be set (from controller or from service) so I can manipulate this list in a cached way (keep it local and update it occasionally)?
I am coming from C# world and there I always used entity objects (e.g. User, Product, Item etc.) populate those object in a loop and store it in a list. I can't find a way how should I use this approach in angular too and if yes should that be service with properties only?
I use one service to keep the list and one service to contain CRUD functions.
If I load data in $scope
from my model how to update that scope later if some other part of code change data in my model?
Change can come from another controller or be updated via SignalR for example.
Also as I heard when I update data on view as $scope
should be readonly I need to update service and again how and when to update $scope
then?
I am sorry if my question is too noob but I would be thankful if someone can help me to understand where to keep what in angular?