The topic has been already covered in the Internet multiple times. If you’re looking for a comprehensive article, just google it up. This blog post is intended to be used more like a handnote or a cheatsheet, so I’ll try to keep it extremally concise.
The following example represents a
Task class which contains two properties: a textual content and a (theoretically) read-only array of hashtags. The latter property is automatically regenerated by extracting hashtags from the task’s content via a regex expression.
Long story short, defining a class in AngularJS looks as follows:
There is no way to define a purely private property (or at least I don’t know one). In my opinion, the best that can be done is to follow a specific naming convention. Names of properties that are not intended to be directly modified should start with an underscore character (
_content) and be accompanied by appropriate getters and setters.
To become usable in a controller, the class has to be injected as any other module and we’re done: