0
我想從我的控制器訪問一個變量,所以我可以在我的服務中使用它!我認爲,如果我製作了全球變量,我可以在我的服務中訪問它,但它不是嘿嘿。變數是店鋪 btw!從控制器訪問變量以服務| Angular 1.5
這裏是我創建的變量:
$scope.$on('$ionicView.enter', function (e) {
ecommercer.getData().then(function (s) {
$scope.shops = s.vendors
$scope.categories = s.categories
$ionicScrollDelegate.resize()
if ($stateParams.id && $stateParams.id.length > 1) {
$ionicTabsDelegate.showBar(false)
ecommercer.saveShopById($stateParams.id)
$state.go('tab.ecommerce.shop')
}
})
})
這裏(同一個文件),我用我的變量:
$scope.$watch('selectedCategory.name', function (newval, oldval) {
if ($scope.selectedCategory.name != undefined) {
$scope.shops = ecommercer.filterByCategory(newval.id)
} else {
$scope.shops = ecommercer.filterByCategory(null)
}
})
這裏就是我創建相同的可變在我的服務文件:
app.service('ecommercer', function (eapi, $q, storage, $http, endpointHandler, $state, $rootScope, $filter) {
var products, categories, vendors, shops
最後,這裏是當我想在我的服務來使用它:
function filterByCategory(categoryid, shops) {
if (!categoryid) return shops
console.log(shops);
var newlist = []
for (var s in shops) {
if (shops[s].category.split(',').length > 0) {
for (var c in shops[s].category.split(',')) {
if (shops[s].category.split(',')[c] == categoryid) {
newlist.push(shops[s])
}
}
}
}
return newlist
}