2016-07-28 77 views
0

欲用於可變replacers從控制器PhraseListCtrlcontrollers.js移動邏輯CategoryServiceservices.js從控制器刪除變量邏輯,它移動到服務

的當前設置(其正常工作),如下:

在controller.js

function PhraseListCtrl($scope, $stateParams, CategoryService) { 

     $scope.categoryId = $stateParams.categoryId; 
     $scope.category = $stateParams.category; 

     CategoryService.getCategoryDetail($scope.categoryId).then(function(dataResponse) { 
     $scope.categoryDetail = dataResponse.data; 

     var replacers = { 
      '{{group}}': '<a class="button button-small button-outline button-positive button-side-padding">group</a>', 
      '{{attribute}}': '<a class="button button-small button-outline button-assertive button-side-padding">attribute</a>', 
      '{{factor}}': '<a class="button button-small button-outline button-assertive button-side-padding">factor</a>', 
      '{{person}}': '<a class="button button-small button-outline button-positive button-side-padding">person</a>' 
     } 

     console.log(replacers); 

     $scope.categoryDetail.forEach(function(e) { 
      Object.keys(replacers).forEach(function(key) { 
      e['phrase_filter'] = e['phrase_filter'].replace(new RegExp(key, 'g'), replacers[key]); 
      }) 
     }) 

     }); 

    } 

在services.js

function CategoryService($http) { 

     this.getCategoryList = function() { 

      return $http({ 
       method: 'GET', 
       url: 'http://127.0.0.1:8000/api/category/', 
      }); 

     } 


     this.getCategoryDetail = function (categoryId) { 

      return $http({ 
       method: 'GET', 
       url: 'http://127.0.0.1:8000/api/category/' + categoryId, 
      }); 

     } 

    } 

但是,當我移動邏輯aro一點點,它似乎無法訪問變量replacers了。

在controller.js

function PhraseListCtrl($scope, $stateParams, CategoryService) { 

     $scope.categoryId = $stateParams.categoryId; 
     $scope.category = $stateParams.category; 

     CategoryService.getCategoryDetail($scope.categoryId).then(function(dataResponse) { 
     $scope.categoryDetail = dataResponse.data; 

     var replacers = CategoryService.getCategoryFilter; 

     console.log(replacers); 

     $scope.categoryDetail.forEach(function(e) { 
      Object.keys(replacers).forEach(function(key) { 
      e['phrase_filter'] = e['phrase_filter'].replace(new RegExp(key, 'g'), replacers[key]); 
      }) 
     }) 

     }); 

    } 

在services.js

function CategoryService($http) { 

    this.getCategoryList = function() { 

     return $http({ 
      method: 'GET', 
      url: 'http://127.0.0.1:8000/api/category/', 
     }); 

    } 


    this.getCategoryDetail = function (categoryId) { 

     return $http({ 
      method: 'GET', 
      url: 'http://127.0.0.1:8000/api/category/' + categoryId, 
     }); 

    } 

    this.getCategoryFilter = function() { 

     var replacers = [{ 
      '{{group}}' : '<a class="button button-small button-outline button-positive button-side-padding">group</a>', 
      '{{attribute}}' : '<a class="button button-small button-outline button-assertive button-side-padding">attribute</a>', 
      '{{factor}}' : '<a class="button button-small button-outline button-assertive button-side-padding">factor</a>', 
      '{{person}}' : '<a class="button button-small button-outline button-positive button-side-padding">person</a>' 
     }] 

     return replacers; 

    } 

} 

我在做什麼錯?

回答

1

你得到了該行的函數參考:

var replacers = CategoryService.getCategoryFilter; 

你應該調用的函數這樣

var replacers = CategoryService.getCategoryFilter(); 

應該這樣做。

編輯

而且它發生,你正在創建的服務replacers數組,但您要訪問的對象。

+0

謝謝,這已經解決了這個問題。 – methuselah