16.07.2017 Views

AngularJS Essentials

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Scope<br />

if (newValue == oldValue) return;<br />

$scope.plateCounter++;<br />

});<br />

});<br />

Best practices using the scope<br />

The scope is not the model itself—it's just a way to reach it. Thus, the view and<br />

controller layers are absolutely free to share any kind of information, even those that<br />

are not related to the model, and they only exist to fulfill specific layout matters such<br />

as showing or hiding a field under a determined condition.<br />

Be careful about falling into a design trap! The freedom provided by the scope can<br />

lead you to use it in a wrong way. Keep the following advice in mind:<br />

"Treat scope as read-only inside the view and write-only inside the controller<br />

as possible."<br />

Also, we will go through some important advice about using the scope:<br />

Avoid making changes to the scope directly from the view<br />

This means that though it is easy, we should avoid making changes to the scope by<br />

creating or modifying its properties directly inside the view. At the same time, we<br />

need to take care about reading the scope directly everywhere inside the controller.<br />

The following is an example from the faq.html file where we can understand<br />

these concepts in more detail:<br />

Open<br />

<br />

<br />

FAQ<br />

<br />

<br />

You are in the Frequently Asked Questions!<br />

<br />

<br />

Close<br />

<br />

<br />

[ 106 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!