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

Encountering an "XML Parsing Error: no element found Location" error in AngularJS $http when JSON results are expected on a GET request

Whenever I try to make an angularjs $http GET request, I am faced with an XML parsing issue. Here is the code snippet for the $http call: $http({ method: 'GET', url: "myapp/api/items" + itemId }); The error message I receive is as fol ...

What is the best way to deal with empty arrays during serialization of modified form elements only?

Managing a form with numerous multi-select fields can be challenging, especially when dealing with paged ajax. With each select having a unique ID and named accordingly as values[foobar1][], values[foobar2][], values[foobar3][], and so on, it's crucia ...

Experiencing a missing handlebars helper error when utilizing a helper within partials in express-handlebars

I have set up custom helpers using express-handlebars like this: const { create } = require("express-handlebars"); // Configuring the handlebars engine const hbs = create({ helpers: require("./config/handlebars-helpers"), }); app.engi ...

Passing PHP data to a JavaScript file using JSON格式

I have a query regarding how to pass php variables and send their values to a js file using json. Here is what I currently have: <?php $data = array('artist' => $artistname, 'title' => $songname); echo json_encode($data); // ...

Utilize jQuery's animate method to scroll to the top after toggling a class in Vue

I am in the process of developing a FAQ page using vue.js Here is what I have implemented so far: <li v-for="i in items | searchFor searchString" v-on:click="toggleCollapse(i)" :class="{ collapsed: i.collapse, expanded: !i.collapse }" > <p> ...

Ways to access the files attribute in an input tag in AngularJS without relying on getElementById

I am currently working on file uploads using AngularJS and I have a question regarding how to retrieve input files similar to regular JS. What I want to achieve: HTML: <input type="file" name="file" id="fileImg" accept="image/*"> JS: var file ...

Does type conversion have a specific ranking of preference?

When working with JavaScript, it's important to note that the language automatically converts types when necessary. For instance, if you have an expression like "8" * "3", JavaScript will convert the strings to numbers and calculate the result as 24. ...

Mapping the Way: Innovative Controls for Navigation

Currently, I am utilizing the HERE maps API for JavaScript. However, I would like to customize the design of the map controls similar to this: Below is an example for reference: HERE EXAMPLE Is it feasible to achieve this customization? ...

Exploring methods to retrieve data from the web3 platform through Node.js

Is there a way to retrieve token information such as name, symbol, and decimals using Nodejs in conjunction with web3js? ...

Steps to generate a script that tracks the number of visible divs and dynamically updates when a user applies a filter

I'm facing challenges in developing a script that can count and display the number of divs selected by the user compared to the total number of available divs. If you'd like to see the whole problem, check out this JSFiddle: http://jsfiddle.net/ ...

Upgrade from AngularJS to Angular 5 in Eclipse

Currently, I have a web application built in AngularJS, but I am looking to transition it to Angular 5. The existing webapp is running on Eclipse with a Tomcat server and has Java as the backend language. I have created a new version of the web applicati ...

Restrict the number of rows displayed in the ui-grid to the maximum

I'm looking to adjust the height of the ui-grid so that it fits the size of 3 rows. After reviewing the ui-grid documentation, I noticed that there is a minRowsToShow property available, but couldn't find an equivalent for maxRowsToShow. I' ...

Troubleshooting custom bower dependency with ng-translate: encountering a 404 error while trying to load the JSON resource

I have successfully implemented a directive using angular-translate with the following configuration: $translateProvider.useSanitizeValueStrategy('escape'); $translateProvider.useStaticFilesLoader({ prefix: 'i18n/messages_', su ...

Enhancing the appearance of the content editor with a personalized touch

I am working with a standard content editor that utilizes an iFrame as the text area. Upon changing dropdown options, it triggers the following command: idContent.document.execCommand(cmd,"",opt); Where "idContent" refers to the iFrame. One of the dropd ...

What is the best way to concatenate a data object?

This task should be quite straightforward. Using Vanilla JS, I am trying to update the content of a span element with the session ID obtained from a function call. Here's an example: sessionId = 0_77b1f7b5-b6c8-49a0-adbc-7883d662ebba document.getEle ...

Error message triggered by Yubikey in WebAuthn Authentication API

Having trouble with implementing WebAuthn support on a new website, specifically encountering issues during the navigator.credentials.get() call. The client browser being used is Firefox 85.0 on Fedora 33, and the server is Apache httpd also on Fedora 33. ...

Setting the offset for panResponder with hooks: A step-by-step guide

While exploring a code example showcasing the use of panResponder for drag and drop actions in react native, I encountered an issue with item positioning. You can experiment with the code on this snack: The problem arises when dropping the item in the des ...

The issue with Bootstrap 5 navbar dropdown links not functioning on screens smaller than 576px

I'm completely new to this. I managed to set up a functional navbar with a dropdown feature that consists of 3 links. When expanded, each dropdown link displays a card upon clicking, revealing contact information. Everything works perfectly fine...exc ...

Breaking down numerous requests into chunks in JavaScript: A step-by-step guide

I am facing a situation where I have multiple requests to make, but they cannot all be called simultaneously. Therefore, I came up with a solution to split the set of requests into chunks of 10. I'm curious about how I can handle making these 10 reque ...

Understanding how events propagate in three.js

I am currently working on a scene that consists of multiple objects. To manipulate the selected object, I am using an orthographic camera controller. Specifically, I want to rotate the object with the mouse by pressing shiftKey + 1 (rotation around its own ...