Binding object attributes in view or use an API from service?

19 views
Skip to first unread message

César Barone

unread,
Jun 12, 2015, 1:30:47 PM6/12/15
to ang...@googlegroups.com
Hi,

I am implementing a AccountService that is storages an account object that contains some informations about user account and used in some parts of my application . I asking for my self what is the better way to access account object properties. I created two plukers that demonstrates two differente approaches and i need i help of the pros an cons of each approach.

In this plunker i am binding account properties direct from view

In this plunker, i am created an API to access account properties


Any one suggests another approach?

 Thanks

Alain Chautard

unread,
Jun 12, 2015, 5:12:08 PM6/12/15
to ang...@googlegroups.com
Both options look good to me. I usually avoid to use direct method calls in my Angular expressions though as change listeners basically have to reevaluate that method call way too often to make sure the returned value has not changed. If you use a $scope variable instead, then that issue is gone plus you would get the additional benefit of two-way data binding.

What I would do: 
- Init a $scope.credits variable with your service call when your controller is initialized
- Use the $scope.credits variable in HTML instead of method calls to your service

Sander Elias

unread,
Jun 13, 2015, 2:58:27 AM6/13/15
to ang...@googlegroups.com
Hi César,

First, you should read trough the styleguide, and switch to the controllerAs syntax.
Then I would go for the first style, as this is less coding, and it expresses more cleanly what you are trying to do. If you like to use getters, you can expose getters from your factory too.

Regards
Sander

César Barone

unread,
Jun 13, 2015, 2:05:18 PM6/13/15
to ang...@googlegroups.com
Guys, 

I will change to use controllerAs syntax. I choose the firts approach. It is more performatic and clean.

Thanks a lot!

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/xMKlkJ4zrAE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages