I am trying to use slice() method on an object array in AngularJS but its says 'slice is not a method'.
Here is the code
.controller('AppCtrl', function ($scope, Faq) {
$scope.filteredFaqData = [];
$scope.currentPage = 1;
$scope.numPerPage = 5;
$scope.maxSize = 3;
$scope.faqData = {};
Faq.get().then(function (msg) {
$scope.faqData = msg.data;
});
$scope.$watch('currentPage + numPerPage', function () {
var begin = (($scope.currentPage - 1) * $scope.numPerPage)
, end = begin + $scope.numPerPage;
$scope.filteredFaqData = $scope.faqData.slice(begin, end);
});
});
I am getting the data from json file in $scope.faqData using a service and it is working.
But slice method is not working in the console it is giving error "$scope.faqData.slice" is not a method.
faqDatais an object.$scope.faqData = {};. Unless your service is mutating it. Slice is a method available to arrays. (even though technically an array is an object with numeric property keys and a special relationship withlengthbut you get my point).factory('Faq', function ($http) { return { get: function () { console.log("inside function"); return $http.get('/Json/faq.json'); } }; }). The service is giving back an array of objectsfaqDatavariable to the type you intend to use with it. If the watch is fired before the promise resolves you will be trying to call.sliceon an object as it hasn't been mutated at that point.