2016-07-08 91 views
1

我有幾個控制器,具有不同的模板工作:避免在不同的控制器重複範圍變量AngularJS

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' 
     }). 

論文控制器的所有已$範圍變量叫cities 例如,第一個:

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; 
     ... 

第二個:

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; 
     ... 

可以在不同的控制器中重複使用$ scope變量嗎? 我應該如何重構代碼才能避免重複?

+0

使用可*控制器*語法,使控制器的別名 –

回答

1

您可以創建一個工廠:

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

    return { 
     getCities: getCities 
    } 
}); 

然後在你的控制器:

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; 
    }); 
     ... 
2

如果城市不打算像更改每個控制器使用的服務低於

您的服務應該如下面

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 

        }) 
     }); 

控制器使用上述服務

app.controller('ctrl',function($scope,myService){ 
      myService.getCities().then(function(data){ $scope.cities = data;}) 
    }) 
0

如果json城市對所有控制器都很常見。

一種選擇是將跨控制器使用的通用對象移動到根視鏡refer

其他選項將存儲城市json在服務本身讀取here

1

你應該做創建一個工廠這樣。原諒語法,但你明白了。

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

}]);

,並在控制器注入工廠和獲取數據

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