Is it a good practice? Is it a right approach?
function UsersCtrl ($scope, $window, $http) {
$scope.Users = {
data : [],
load : function () {
$http.get('users/').success(function (data) {
$scope.Users.data = data;
});
},
emailUser : function (user) {
// send email (just an example)
}
};
}
An the HTML:
<li ng-repeat="user in Users.data">
</li>
<a href="#" ng-click="Users.load()">
Load
</a>
<a href="#" ng-click="Users.emailUser(user)">
Email
</a>
I'm worried about performance!
I searched on the web and I didn't see any code like that. Most of them separate the methods and properties in a specific $scope model.
Thanks in advance!
This particular code won't affect performance, but it will affect testability and readability of the app. Plus, this is in confrontation with Separation Of Concerns principle and standard MVC pattern.
Your Users object should be defined inside a service factory and than injected wherever required.
If you were to leave it defined inside your controller than you would have to redefine it again if you needed the same Users object inside some other controller.
That said, it's not a surprise that you haven't seen any code like this.