Chapter 7. AngularJS Models
AngularJS models are held in the $scope
object. In AngularJS, $scope
is used to gain access to the model related to a particular controller. $rootScope
is a parent scope that can be used to save and access model properties that span multiple controllers. The use of $rootScope
is highly discouraged in most designs, however. There is only one $rootScope
in an application. $scope
is a child scope of $rootScope
.
A properly designed AngularJS application will have little or no use for $rootScope
to store model properties. In this chapter we will focus only on $scope
, used to store the model retrieved from REST services.
Public REST Services
The REST services used for this chapter are available at http://nodeblog-micbuttoncloud.rhcloud.com/NodeBlog. The services are open to the public and written in JavaScript using Node.js, ExpressJS, and MongoDB. In Chapter 11, you will deploy the same REST services with your AngularJS blog application as a MEAN stack (MongoDB, ExpressJS, AngularJS, and Node.js) application. You will then deploy the MEAN stack to the cloud using a free RedHat OpenShift account.
The following excerpt shows how AngularJS services access the REST services used for this chapter. The REST services return the same JSON that was previously hardcoded in the controllers:
/* chapter7/services.js excerpt */
$resource
(
"http://nodeblog-micbuttoncloud.rhcloud.com/NodeBlog/blog/:id"
...
$resource
(
"http://nodeblog-micbuttoncloud.rhcloud.com/NodeBlog/blogList" ...
Get Learning AngularJS now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.