0
欲用於可變replacers
從控制器PhraseListCtrl在controllers.js
移動邏輯CategoryService在services.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;
}
}
我在做什麼錯?
謝謝,這已經解決了這個問題。 – methuselah