Hacker News new | ask | show | jobs
by EdwardMSmith 4908 days ago
As a person that switched from Backbone to Angular, can you explain models in Angular (from a Backbone standpoint)?

I've looked over the docs, and as far as I can tell there really isn't a model in Angular - not anything concrete anyway.

The docs seem to explain that any JS object is a model as long as you make some reference to it.

Or, is an angular Controller (+ some JS data structure) really a model.

Its quite confusing to me.

2 comments

I can't explain in terms of Backbone but I'll do my best to clear things up for you anyway. In Angular, you have a magic $scope variable that is available both in your controller function, and in the section of HTML that the controller is bound to. Now I can set any attributes I want to the $scope variable and they will act like models for that controller.

For example. Within my controller, I set $scope.username to "alex" and this will update any HTML bound to the value "username", within the scope, to "alex". Similarly, I can set, say, $scope.settings = {user: {username: "alex"}} and this will update any HTML elements (within the scope) bound to settings.user.username to "alex".

These are very simple examples, but they show the beauty of having plain JS objects as your "models".

Angular model is just any normal js object you want, then via two way binding it gets displayed without you doing any work. Freakin awesome.