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

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -