javascript - Keeping the data in a service and displaying it on a view -


i want clean controller , move data fetching logic service, there wouldn't following code in controller:

myservice.fetchdata().then(funtion(response) {     self.data = response.data; }); 

this achievement far: link

is practice? problems rise this?

also how can remove app.users() call in template , leave app.users variable?

it practice move logic service, should assign users property (model) instead of relinking service controller:

var self = this;  this.fetchusers = function() {     // assign result 'users' property, can used     // in view     userservice.fetchusers().then(function(data){         // success         self.users = data;         // or, because remember users in service:         // self.users = userservice.getusers();     }); } 

and view:

<button ng-click="app.fetchusers()">get users</button> <github-table columns="app.columns" rows="app.users"></github-table> 

edit

the service:

function fetchusers () {     // return $http call (which promise)      // .then can used     return $http         .get('https://api.github.com/users')         .success(function (response) {             users = response;         }); } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -