How avoid redundant variable in angularjs -
i have 2 controllers.
the first 1 listcontroller.js:
app.controller('listctrl', function($scope, $http, $controller, $window) { $scope.user = [{id:1,name:'rina'},{id:2,name:'anna'},{id:3,name:'yuna'}]; });
and second editcontroller.js
app.controller('editctrl', function($scope, $http, $controller, $window) { $scope.user = [{id:1,name:'rina'},{id:2,name:'anna'},{id:3,name:'yuna'}]; });
as u can see. used variable $scope.user
in both controller. want used once. mean want avoid redundant codes. how can call variable in of controller. suggestions?
you can add service, responsible providing data.
app.service('usersservice', function() { this.getusers = function() { return [{id:1,name:'rina'},{id:2,name:'anna'},{id:3,name:'yuna'}]; } });
then in controllers can put this:
app.controller('listctrl', function($scope, $http, $controller, $window, usersservice) { $scope.user = usersservice.getusers(); });
Comments
Post a Comment