Hi guys!

Some time ago I watched the Angular Best Practices Video 
<https://www.youtube.com/watch?v=ZhfUv0spHCY&list=TL3Td3jaA2QI3oyh7s50-KGGIKzwhR9Dr->
where Miško Hevery make the following, very important statement: "Scope is 
not the model, scope has references to the model". Later he also states 
that "(when)... using ng-model, there's got to be a dot in there somewhere".

So recently I'm applying this rule to my model and developed a habit of 
creating a *"$scope.model = {}"* object which is then populated with 
whatever is necessary in the view. For instance, to show selectable list of 
users I might write:

*$scope.model = {*
*   users: [ { id: 1, username: "Łukasz Bachman", age: 29 }, { id: 2, 
username: "John Doe", age: undefined } ],*
*   selectedUserId: undefined*
*};*

And then ofcourse I iterate over *ng-repeat="user in model.users"* and 
create some click handler like so: *ng-click="model.selectedUserId = 
user.id".*

This looks fine for me and haven't found any issues with this approach so 
far. But lately the first sentence that I quoted has been 
constantly roaring in my head. Am I really using it right? Perhaps I should 
only keep references to the model there, not attach the data to this 
universal "model" container! An example:

*var usersArray = [ { id: 1, username: "Łukasz Bachman", age: 29 }, { id: 
2, username: "John Doe", age: undefined } ];*
*var selectedUserId = undefined;*

*$scope.model = {*
*   users: usersArray,*
*   selectedUserId: selectedUserId*
*};*

Can anyone tell me if this is having any benefits over my current approach? 
I'm most interested in:

   - does either of the solutions hides some bugs or flaws?
   - is the overhead of watching the scope is higher/lower in any case 
   (performance implications)?
   - is there any recommended way of doing this?

If you have some references that explain this in depth, then please let me 
know. I'll be glad to read and learn more about it, but if some of Angular 
Masters can simply say "you are doing it wrong/right" then I'd highly 
appreciate it.

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to