1

I've got few controllers working with different templates:

curryApp.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider.
        when('/', {
            templateUrl: 'table_index.html',
            controller: 'CurryListCtrl'
        }).
        when('/cities/:cityId/', {
            templateUrl: 'template-total.html',
            controller: 'CurryDetailCtrl'
        }).
        when('/cities/:cityId/mobile/', {
            templateUrl: 'template-mobile.html',
            controller: 'CurryMobileCtrl'
        }).
        when('/cities/:cityId/online/', {
            templateUrl: 'template-online.html',
            controller: 'CurryOnlineCtrl'
        }).

All of theses controllers have $scope variable called cities For example, the first one:

curryControllers.controller('CurryDesktopCtrl', ['$scope', '$routeParams', '$http', '$route',
  function($scope, $routeParams, $http, $route) {
    $scope.cityId = $routeParams.cityId;
    $http.get('json/cities.json').success(function (data) {
      $scope.cities = data;
      ...

The second one:

curryControllers.controller('CurryOnlineCtrl', ['$scope', '$routeParams', '$http', '$route',
  function($scope, $routeParams, $http, $route) {
    $scope.cityId = $routeParams.cityId;
        $http.get('json/cities.json').success(function(data) {
        $scope.cities = data;
        ...

Is it okay to have repeated $scope variables in different controllers? How should I refactor the code in order to escape duplicates?

1
  • use can controller As syntax and make alias of the controller Commented Jul 8, 2016 at 5:27

4 Answers 4

2

If cities are not going to change for every controller use services like below

Your service should look like below

 app.service('myService',function($http,$q){
      var cities=null;
      this.getCities=function()
                     {

                         var deferred = $q.defer()
                         if(this.cities==null){
                         $http.get('json/cities.json')
                         .then((response) => {deferred.resolve(response);cities=response;})
                         .catch((error) => deferred.reject(error))
                         }
                         else { deferred.resolve(cities)}
                         return deferred.defer  

                     })
        });

Use the above service in the controller

  app.controller('ctrl',function($scope,myService){
           myService.getCities().then(function(data){ $scope.cities = data;})
  })
Sign up to request clarification or add additional context in comments.

Comments

1

You can create a factory:

.factory('citiesFactory', function($http) {
   function getCities(){
      // return promise from function
      return $http.get("json/cities.json");
   }

   return {
      getCities: getCities
   }
});

then in your controller:

curryControllers.controller('CurryDesktopCtrl', ['$scope', '$routeParams', '$http', '$route', 'citiesFactory'
  function($scope, $routeParams, $http, $route, citiesFactory) {
    $scope.cityId = $routeParams.cityId;
    citiesFactory.getCities().then(function(response){
       $scope.cities = response.data;
    });
      ...

Comments

1

You should do create a factory like this. Forgive the syntax, but you get the idea.

app.factory('Cities', function ($http) {
return {
    'getData': function() {
         $http.get('json/cities.json').success(function (data) {
           return data;
          }
          .error(function(error){
           return [];
          }
    }
};

}]);

and in your controller inject the factory and get the data

curryControllers.controller('CurryOnlineCtrl', ['$scope', '$routeParams', '$http', '$route', 'Cities'
  function($scope, $routeParams, $http, $route) {
    $scope.cityId = $routeParams.cityId;
    $scope.cities = Cities.getData();
    ...

Comments

0

If the cities json is common for all the controllers.

One option would be to move the common object used across controllers into rootscope refer.

Other option would be to store the cities json in the service itself read here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.