Unable to refresh scope variable within the scope function as expected

Updating my chart data using chartJS and its angular-chart wrapper has been quite the challenge for me. When I click a button, new data is added to the chart without any issues.

However, I encountered a problem when trying to update the chart after a scroll event. The function call worked perfectly fine and invoked the same function as before, but strangely, my $scope.valuesData remained unchanged. In an attempt to solve this issue, I included a $scope.$apply() after the function, which ultimately redrawn the entire chart with the correct values.

Here are my questions:

  1. Why does the $scope.chartValues not get updated when I call the AddData function from the controller (compared to when I call it from the DOM)? Could this be related to a data-binding issue?

  2. Is there a way to have a function that updates only a specific scope variable instead of refreshing the entire scope?

Here's a snippet of my code:

HTML:

<ion-scroll zooming="false" direction="x" delegate-handle="scroller" on-scroll="getScrollPosition()" has-bouncing="true" scroll-event-interval="30000">
  <div class="chart_wrapper" ng-style="{ 'width': myWidth + '%' }"> 
      <canvas  class="chart-bar" chart-data="data2" chart-labels="labels" chart-dataset-override="datasetOverride2" chart-options="options2" ng-click="goToMetrics()" ">
      </canvas>    
 </div>
</ion-scroll>

<button class="button button-small button-calm" ng-click="addValues()">Add Data </button>

JS (controller):

  $scope.getScrollPosition = function(){
  var pos =             $ionicScrollDelegate.$getByHandle('scroller').getScrollPosition().left ; 
  if (pos == 0 && $scope.flag == -1){
     $scope.flag = 0;
     //setTimeout($scope.addValues(), 1000);
     $scope.addValues();
  }
  if (pos < 0){
    $scope.flag = -1;
  }
 };


  $scope.addValues = function(){
  console.log('add');
  // Retrieve values
  var week = Graph.getData();
  var pp = $scope.chartData; 
  var mm = $scope.labels;

  // Add values to the chart and update the chart width 
  var tmp = Graph.addBegin(pp, mm,  week);
  $scope.valuesData = tmp.values; 
  $scope.myWidth = $scope.myWidth + tmp.length * 4 ; 
 };

Any help or insights would be greatly appreciated.

Answer №1

  1. When utilizing setTimeout outside the angular context, opt for the $timeout version instead.
  2. Incorrect. Utilize $scope.$digest() to monitor changes within the current scope only, not throughout the entire $scope tree.

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

Toggle the Editable Feature in AngularJS JSON Editor

Currently, I'm utilizing a library called ng-jsoneditor that is available on GitHub. I am attempting to switch the state of an element from being editable to being read-only. To indicate that an element should be read-only, I need to specify the onE ...

Angular JS | Sending information to two separate controllers when clicked

I have a series of projects displayed in divs using ng-repeat. Each div contains a link with the project's id. My goal is to bind the project id on ng-click to update a factory. This will enable me to share the clicked project's id with another ...

Sort information based on the alphabetical order of words using AngularJS

I'm working with AngularJS and I'm looking to filter data alphabetically in AngularJS. Any assistance would be greatly appreciated! This is the code snippet I've been using: <script src="~/Scripts/angular.min.js"></script> < ...

What is the best way to change a variable in an AngularJS view?

My Request In my application, I have implemented 3 views, each with its own controller. The first view is the home screen, and from there the user can navigate to view 2 by clicking on a div element. On view 2, the user can then move to view 3 by clicking ...

Angular is having trouble parsing Json data which is resulting in a blank screen

I'm struggling with parsing a JSON file. In order to parse an array, I'm using the following code snippet: {"records":[{"id":"1","first_name":"John","last_name":"Doe"},{"id":"2","first_name":"Jane","last_name":"Doe"},{"id":"3","first_name":"Jo ...

Encountering the error message 'Invalid cast specified' while using SqlDataReader in MVC

I kindly request not to categorize this inquiry as a DUPLICATE QUESTION. Despite my efforts to follow all the suggested solutions, I am still encountering this error. Is there anyone who can provide insight on where this error originates from? Below is th ...

Adding a fresh element to an object array in TypeScript

How can we add a specific value to an array of objects using TypeScript? I am looking to insert the value 1993 into each "annualRentCurrent" property in the sample object. Any suggestions on how to achieve this in TypeScript or Angular? Thank you! #Data ...

One or multiple web browsers set in the Browserslist of the project

I have recently started working with the Ionic framework and encountered some warnings while setting up my application. Can anyone help me with a solution? [ng] When starting the Ionic application, I received the following warnings: [ng] One or more browse ...

Tips for adding an array to an array of objects with AngularJs

I'm facing an issue with the array structure in my code. Here's what I currently have: $scope.arrayList=[{FirstName:"",LastName:""}]; $scope.Address=[{address:"",PhoneNumber:""}]; What I want to achieve is to push the $scope.Address array into ...

What is the best way to utilize the outcome of the initial API request when making a second API call in AngularJS

Looking to utilize the response of a first API call in a second API call. The situation is such that I need to fetch data from the first API and use it in the second API call. I believe a synchronous API call would be necessary. While attempting to imple ...

AngularJs is being used to extract data from Firebase with the help of $value, $id, and

I have been working on retrieving data from Firebase in my HTML using AngularJS. Everything is functioning well, however, when I access the child node, the data is displayed in an unexpected format. Please refer to the images below for more details: This ...

Receiving a 500 (Internal Server Error) in AngularJS when making a $http POST request

Being a novice in the world of Ionic and Angular, I am currently working on a project using the Ionic framework with Angular.js. My goal is to make a WebApi call using the $http post method. I have referred to the solution provided in this ionic-proxy-exam ...

What is the process for defining the scope within the Facebook ionic authentication provider?

As I implement the Facebook Ionic authentication provider, I am wondering: where and how should I configure the scope to request user consent for accessing specific data from their Facebook profile in my Ionic app? This configuration is being done on the ...

Is there a dependable resource for mastering Protractor along with the Jasmine Framework in Eclipse using JavaScript?

Starting a new role at my organization where I will be testing Angular JS applications. Can anyone recommend a good website for learning PROTRACTOR with JAVASCRIPT using the JASMINE Framework? (Would prefer if it includes guidance on Eclipse IDE) Thank yo ...

AngularJS - Show alert message when email format is incorrect on Textbox Blur

I have set up AngularJs email validation in the following way: <div class="control-group" ng-class="{'has-success': contactform.contactemail.$valid && submitted,'has-error': contactform.contactemail.$invalid && s ...

ng-switch only 1 element within an ng-repeat loop

Within my ng-repeat, each row features an edit button. When the user clicks on the edit button, the row's element will be updated using ng-switch. For instance: <div>{{sample.name}}</div> change to <input type="text" ng-model="{{sampl ...

Encountering issues with AngularJS ng-click and $http post functionality

ReactJS theApp.controller('MenuSideController', ['$scope', 'CategoryService', function ($scope, CategoryService){ CategoryService.getList() .success(function(list){ $scope.list = list; }); $scope.me ...

Updating $scope from another controller in AngularJS

I am facing an issue where I need to update the $scope inside a directive's link function. Here is what my controller and directive look like: $scope.current = 0; angular.module('myAPP') .directive('post', function() { ...

Show Timing on the Y-Axis - Bubble Graph

Recently, I stumbled upon the Bubble Chart feature in ng2-charts. I am trying to display data based on time on the Y-axis and values on the X-axis. My dataset consists of x:[10,35,60], y:["7.00 AM"], with r having the same value as x. However, the sample d ...

Having difficulty with Angular's ng-options feature in a Select element

I'm currently tackling an issue with using ng-options to iterate through an array of objects and display specific properties in a select element. Upon querying the api/admin endpoint, I receive JSON data containing details of all users holding the ad ...