AngularJS is not triggering the $watch function

I'm facing an issue with the $scope.$watch function, as it's not being triggered when expected. In my HTML document, I have a paginator using bootstrap UI:

<pagination  total-items="paginatorTotalItems" items-per-page="paginatorItemsPerPage" 
   page="paginatorCurrentPage" max-size="paginatorSize" class="pagination-sm" 
   boundary-links="true">
</pagination>

There's a section where my items are displayed (for which I need the paginator):

<div ng-show="reviews" ng-repeat="review in reviewsPerPage">
...
</div>

And here's the corresponding Controller:

...
$scope.reviewsArray = [];
$scope.paginatorItemsPerPage = 1;
$scope.paginatorSize = 3;
$scope.reviewsPerPage = [];
$scope.paginatorTotalItems = $scope.reviews.result.total;

       // Converting Restangular object to Array
       for (var i = 0; i < $scope.paginatorTotalItems; i++) {
           $scope.reviewsArray.push($scope.reviews.result.reviews[i]);
       };

       $scope.paginatorCurrentPage = 1;

$scope.$watch('paginatorCurrentPage', function () {
               var begin = (($scope.paginatorCurrentPage - 1) * $scope.paginatorItemsPerPage);
               var end = begin + $scope.paginatorItemsPerPage;

               console.log($scope.paginatorCurrentPage);
               console.log(begin + ' ' + end);

               $scope.reviewsPerPage = $scope.reviewsArray.slice(begin,end);

               console.log($scope.reviewsPerPage);
});

To summarize, I'm experiencing a situation where the variable paginatorCurrentPage changes when clicking on the pagination numbers, but the $watch function is not triggered. It only gets called once when setting its value to 1 initially. Despite the visual change in the HTML file:

<p>Current : {{paginatorCurrentPage}}</p>

The issue persists, even after updating to use ng-model instead of page in the paginator. The variable paginatorCurrentPage seems to update visually, but in the controller, it remains at the default $scope.paginatorCurrentPage = 1. This problem still lingers.

Apologies for any language barriers, and thank you for your help!

Edited :

I have made adjustments to the bootstrap UI by replacing 'page' with 'ng-model' in the paginator. However, the issue remains unresolved as the variable paginatorCurrentPage updates only in the view, while in the controller it remains stuck at the default value of $scope.paginatorCurrentPage = 1. Any insights would be greatly appreciated.

Answer №1

Appreciate the feedback from everyone. The issue was related to scope. I made a change in the ng-model of the paginator like this: ng-model="paginatorPage.current"

Additionally, I updated

$scope.paginatorCurrentPage = 1;

to

$scope.paginatorPage = {current : 1};

A special thanks to @Leo Farmer for the helpful advice regarding dots in directives.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Storing the created .wav file on the server using PHP

Is there another way to handle this? How can I manage streaming of a WAV file? I'm currently working on a platform that allows users to create their music compositions and the system outputs a .wav file for each creation. While I can play the mus ...

Find all strings in the array that do not begin with a letter from the alphabet

When a specific button is clicked, I need to filter the example array provided in the snippet below. The expected output should only include elements that do not start with an alphabet. I attempted: const example = ["test", 'xyz', '1 test& ...

Alerts being used by Jquery Validation Engine to showcase errors

In the validation engine, my goal is to have error messages appear in a JavaScript alert box instead of as small tooltips right next to the input fields. $("#main_form").bind("jqv.form.result", function(event, errorFound) { if(errorFound) aler ...

Game Mapping Techniques: Utilizing Spatial Data Structures

In order to efficiently store and retrieve intersecting rectangles, I am currently working on implementing a spatial data structure in JavaScript. My initial approach involves using a Quad Tree to narrow down the search space. However, for dynamic objects ...

Select the send all text option when making a request

Using ajax, I can dynamically fill a drop-down select menu. My next step is to include all the text from the selected options in my request. <select name=".." > <option value="0"> ... </option> <option value="1"> xxx </option ...

How can you identify dynamically created elements within an AngularJS directive?

I have a directive where I need to target specific DOM elements, some of which are dynamically generated in an ng-repeat loop. If I try to select them directly, I only get the static elements. However, if I delay the selection by, let's say, 500ms, I ...

How can I prevent my JSON object from serializing .NET nulls as "null" for object members?

I am currently utilizing WebMethods to retrieve an array of a custom class. When this array is returned from a Jquery .ajax call, it gets serialized into a JSON object that can be utilized with Javascript in my ASP.NET application. The issue I am facing is ...

Retrieving information from Angular directive by utilizing the Express interface

Recently, I have developed an interface using express and mongoose that successfully fetches data from mongodb and sends it to the client side without any issues. However, I encountered some errors when trying to retrieve the data using AngularJS $http met ...

Cross Domain Requests in Internet Explorer: Preflight not being sent

I have come across several similar discussions but none of the solutions provided have worked for me so far. Issue: In our current setup, we have three servers hosting our http-apis - two for testing and one for production. Lately, we have been deployin ...

A guide to submitting a form and navigating to a new page using Angular

In my project, I have created a form where users can input key-value pairs. These pairs are then stored in an array and I want to submit this data to the backend in JSON format without using AJAX. Instead of traditional HTML forms that POST data with keys ...

Learn how to dynamically highlight a table row when any changes are made to it using jQuery

Is there a way to automatically highlight the current table row when any input text or image is changed within that specific row using jQuery? In the given code snippet below, with two rows provided, how can one of the rows be highlighted upon modifying ...

The button functionality gets hindered within the Bootstrap well

I'm trying to figure out what's wrong with my code. Here is the code: https://jsfiddle.net/8rhscamn/ <div class="well"> <div class="row text-center"> <div class="col-sm-1">& ...

Using `require(variable)` is not functional in next-js environment

I'm attempting to display an image using the next-optimised-images module. When I try to include images like this: <img src={require(c.logo)} alt={c.title} /> I encounter the following error: https://i.stack.imgur.com/Jtqh9.png However, when ...

I am facing an issue where the table in my Laravel Vue component is not displaying the data from

Recently, I've been diligently following an instructional series on VUE applications by a highly recommended YouTuber. Every step was meticulously executed until I hit a roadblock out of nowhere. The data from my database refuses to display on the fro ...

Exploring the potential of Angular JS and gapi in building a seamless routed

I'm facing a similar challenge as described in this question. However, the key difference is that I require two controllers for two distinct routes, essentially representing two different tables: ../table1 and ../table2. Each table needs to fetch data ...

Having trouble toggling webcam video in React/NextJS using useRef?

I have created a Webcam component to be used in multiple areas of my codebase, but it only displays on load. I am trying to implement a toggle feature to turn it on and off, however, I am facing difficulties making it work. Below is the TypeScript impleme ...

Eliminating an element from an array depending on the value of its properties

I need to remove an object from my List array by matching its properties value with the event target ID. Currently, I am using findIndex method to locate the index ID that matches the event.target.id. Below is an example of one of the objects in my list a ...

Disallow/bound the position of the marker on Google Maps

Is there a way to restrict the placement of markers on a map? I am looking for a solution that allows me to limit the marker position within a specific area, such as a town, with a radius of 10km. It should prevent users from dragging or creating new mark ...

What is the best way to retrieve the elements stored within the 'this' object I am currently manipulating?

How can I access the elements nested within the 'this' that I am currently operating on? Below is the HTML code that I am currently working with: <div class="expander" id="edu">educational qualifications <ul class="list"&g ...

Exploring the power of Angular JS promises through ng-repeat

My current project involves fetching latitude and longitude coordinates from a postcode using an API, then utilizing those coordinates to retrieve data on street level crimes near that location on a specific date through the UK police API. However, I have ...