In general treat variables in the template as read only. The overall pattern for this is
View << Responsible for displaying data
Controller << Responsible for initializing or hooking up services to be exposed
app.controller("appCtrl", function ($scope, userService) {
// expose return from service into the scope for the view
$scope.user = userService.getCurrentUser();
}
Service << Responsible for doing most of the work
app.service("userService", function ($http) {
return {
getCurrentUser: function() {
// using $http or $resource to get the data and return a promise, when resolved will chain all the way up to the view
}
}
}
You can set things explicitly in the controller
$scope.user = {name: 'Denis', skills: ['Angular', 'HTML', 'CSS']}
}
Generally you only want to do this when setting "basic" variables such as initial UI states. The other case is when you are just experimenting with Angular and fiddling with models and how to display things. Other then that nontrivial models and functions should be placed in services, and hooked into the view via the controller.
A thinking shift with Angular is to embrace promises and scope auto updates. Code as if the data is instantly available, even through you'll be waiting for the server. In the view just write the template {{ myobj.attr }} to be displayed. Controller hooks up the object from the service. When the application first loads myobj won't be there, which is fine, nothing will display. Eventually when the server comes back myobj and attr are set. Since they are on the scope, the $digest cycle will kick in seeing it needs to update. It updates when the data comes back, and everything works. If you need to indicate loading or things that you can use something like: <span ng-hide="myobj.attr">Loading...</span>. When the object is loaded, the ng-hide tag will cause the loading text to disappear for you. I would highly recommend reading about promises and embrace them. It will make your life much easier.
Hope that help,
~Owen