I keep getting the "Error: $injector:unpr Unknown Provider" when running this code. I have looked at a lot of other answers similar to this problem but just can't see where the error is.
Also, am I calling this service correctly with "myService()"?
I am relatively new to Angular, so any help or advice would be appreciated. Thanks in advance.
My Service
angular.module('APP')
.service('myService', ['$scope' function($scope) {
$scope.search = {};
$scope.resetFilters = function () {
// needs to be a function or it won't trigger a $watch
$scope.search = {};
};
// pagination controls
$scope.currentPage = 1;
$scope.totalItems = $scope.videos.length;
$scope.entryLimit = 5; // videos per page
$scope.noOfPages = Math.ceil($scope.totalItems / $scope.entryLimit);
// $watch search to update pagination
$scope.$watch('search', function (newVal, oldVal) {
$scope.filtered = filterFilter($scope.videos, newVal);
$scope.totalItems = $scope.filtered.length;
$scope.noOfPages = Math.ceil($scope.totalItems / $scope.entryLimit);
$scope.currentPage = 1;
}, true);
}]);
My Controller
angular.module('APP')
.filter('startFrom', function () {
return function (input, start) {
if (input) {
start = +start;
return input.slice(start);
}
return [];
};
})
.controller('dbCtrl', ['$scope', '$http', 'filterFilter', 'myService', function ($scope, $http, filterFilter, myService) {
$http.get("js/science.php")
.success(function(data){
$scope.videos = data;
myService();
})
.error(function() {
$scope.data = "error in fetching data";
});
}]);
My Links
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
<!-- CDN's and API's-->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script>
<script src="https://www.youtube.com/iframe_api"></script>
<!-- Services -->
<script src="controllers/myService.js"></script>
<!-- Dependencies -->
<script src="js/ui-router-title.js"></script>
<script src="js/angular-youtube-embed.js"></script>
<script src="js/angular-ui-router.min.js"></script>
<script src="js/app.js"></script>
<!-- Controllers -->
<script src="controllers/headerCtrl.js"></script>
<script src="controllers/dbCtrl.js"></script>
<script src="controllers/scienceCtrl.js"></script>
<script src="controllers/politicsCtrl.js"></script>
<script src="controllers/pageCtrl.js"></script>
<script src="controllers/tabsCtrl.js"></script>
<script src="controllers/modalCtrl.js"></script>
Sorry I should have mentioned, I have a main app file where I have declared my app dependencies using angular.module('APP', []);
As far as not being able to inject $scope into a service, I am really just trying to take the pagination code and make it reusable. Any thoughts on how I can achieve this? Thanks for the quick reply by the way.
Here is the original controller with the pagination code I want to make reusable.
angular.module('APP')
.filter('startFrom', function () {
return function (input, start) {
if (input) {
start = +start;
return input.slice(start);
}
return [];
};
})
.controller('dbCtrl', ['$scope', '$http', 'filterFilter', function ($scope, $http, filterFilter) {
// Link to PHP for mySQL content
$http.get("js/science.php")
.success(function(data){
$scope.videos = data; // Array named 'data'
// FROM HERE
//=====================
$scope.search = {};
$scope.resetFilters = function () {
// needs to be a function or it won't trigger a $watch
$scope.search = {};
};
// pagination controls
$scope.currentPage = 1;
$scope.totalItems = $scope.videos.length;
$scope.entryLimit = 5; // videos per page
$scope.noOfPages = Math.ceil($scope.totalItems / $scope.entryLimit);
// $watch search to update pagination
$scope.$watch('search', function (newVal, oldVal) {
$scope.filtered = filterFilter($scope.videos, newVal);
$scope.totalItems = $scope.filtered.length;
$scope.noOfPages = Math.ceil($scope.totalItems / $scope.entryLimit);
$scope.currentPage = 1;
}, true);
// TO HERE REUSABLE
//=====================
})
.error(function() {
$scope.data = "error in fetching data";
});
}]);