Tips on manually refreshing AngularJS view using ControllerAs syntax?

As I work on creating a user-friendly dashboard with widgets that can be sorted, docked, and floated, I encountered an issue. The controls I am using generate floating widgets as HTML at the bottom of the DOM, outside the controller scope where they were created. This setup makes it challenging to update the view when external components trigger actions that affect the data.

While developing this dashboard controller with the controllerAs syntax, I struggled to find a way to efficiently refresh the view. Numerous directives and external components on the page also influence the visuals, adding to the complexity.

I ideally aim for a seamless process without the need for manual view updates. Utilizing Angular's built-in commands to handle changes within the digest loop would have been ideal, but unfortunately, it was not viable in my situation.

If I were using $scope, updating the view would be straightforward with:

$scope.$digest

Or

$scope.$apply

However, the challenge lies in achieving the same outcome using the controller as:

var vm = this;
vm.array = [item, item];
vm.something = something;

//External component alters something on a vm.variable

vm.update! //How??

Answer №1

When you use 'as', you are specifying how to reference your controller scope within your view.

For example:

<body ng-controller="MainCtrl">
  <p>Hello {{name}}!</p>
</body>

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

Is the same as:

<body ng-controller="MainCtrl as main">
  <p>Hello {{main.name}}!</p>
</body>

Your controller:

app.controller('MainCtrl', function($scope) {
  this.name = 'World';
});

DEMO

This means you can invoke this.$digest or this.$apply just like you would with $scope.

UPDATE

Upon further research, it seems that using $scope.apply() or $scope.digest() might be the correct solution.

Main resource:
AngularJS’s Controller As and the vm Variable
There is a comment addressing a similar question and the author's response:

You can use $scope.$apply() in that case and just inject $scope for that purpose (still using vm for everything else). However, if you switch from using ajax to using Angular's $http, then you won't need to call $apply as angular's $http does that for you. That's what I recommend

Other helpful resources:

  • this vs $scope in AngularJS controllers
  • Angular: Should I use this or $scope
  • Do You Like Your Angular Controllers with or without Sugar?

Answer №2

One way to perform your update is by enclosing it within a $scope.$apply block as shown below:

$scope.apply(function() {
    vm.something = updated_value;
});

To prevent the occurrence of an "Digest already in progress" error, you can use $timeout to wrap the code.

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

Registering the service worker resulted in an error stating "Undefined is not a function"

When attempting to register a service worker using default React code, I discovered that some users were encountering a `TypeError: undefined is not a function` on the line `.then(registration => {` inside the registerValidSW function. Although it works ...

Is a UI routing race problem a concern?

After some observation, I've identified a potential race condition that occurs when switching between states too quickly. For example, let's say I navigate to state A. The directives linked to this state are compiled. This process could take lon ...

Change the function to utilize an HTML class rather than an ID

I am currently working on a resize image function that requires the ID of a file input element as an input. The function takes the image in the form and outputs a resized canvas of the image. However, I recently made changes to the form structure from a st ...

Karma-coverage not detected: however, it is already installed

I utilized the following guide when configuring my Karma-coverage plugin. Additionally, it was locally installed through the package.json file which appears like this: { "name": "myApp", "version": "0.1.0", "devDependencies": { "karma ...

CORS problem in Chrome when using AngularJS and Symfony 3

I've been dealing with a bug on my backend and frontend for the past week. I created a REST API (php/symfony) to manage books and authors. Initially, I had trouble with cross-origin requests on DELETE, which has been resolved. However, now I am facin ...

Having trouble sending JSON data to the server using a POST request

I am encountering an issue while attempting to send JSON data to the server using the fetch API and PHP as the server-side language. The PHP code on the server side is quite simple: <?php header("Access-Control-Allow-Origin: *"); header("Access ...

Facing Hurdles in Starting my Debut Titanium Mobile Project

I recently started using Titanium Studio (version 2.1.0GA on WindowsXP) and I have added the Android SDK to it. However, I am encountering an error when trying to run my first mobile project. The console is displaying the following message: Can anyone off ...

Implement code to execute exclusively on the initial success of react-query

I have a unique scenario where I need to utilize standard useQuery behavior, while also executing a piece of code only on the initial onSuccess event. Although I understand that I can accomplish this using useRef, I am curious if there is an alternative a ...

Difficulty encountered in assigning a value to a variable when utilizing an async function in Node.js

While attempting to retrieve the jwtauthtoken from the user and passing it to a function called getUserId() in the imported JavaScript file, I encountered an issue where the returned value was undefined instead of the decoded ID. The getUserId() function ...

Utilizing the require pattern to integrate JavaScript functionality into HTML

Have: project |- consume_script.js |- index.html Need index.html to be like: <html> <head> </head> <body> <script src="consume_script.js"></script> </body> </html> Issue: consume_script ...

Vue: Utilizing computed properties to monitor changes in offsetHeight of elements

I am working on a component that requires an array of 50 objects to be passed as a prop. <template> <div v-for="(item,index) in items" ref="items" :key="index"gt; // </div> </template> props: ...

What is the best approach to transform a lengthy collection of 'watch' statements into a functioning solution using VueJS?

I am new to vueJS and need some help with optimizing my code. I have a long list of watch functions that are all essentially the same, but I'm not sure how to convert them into a more efficient and functional approach. These watch functions are all r ...

Storing geographic locations in a variable by inputting an address

Currently, I am working on a feature that allows users to input a location and then convert it into longitude and latitude coordinates. These coordinates will be stored in a variable called latlng instead of using preset coordinates. Right now, I have a fu ...

Utilizing Selenium JavaScript to insert a cookie into a request

Trying to add a cookie to the request in Selenium using JavaScript. I followed the documentation at this link, but my code snippet doesn't seem to pass any cookies to the PHP script below on the server. Here is the client-side JavaScript code: var w ...

Refresh Angular controller after ng-click event is triggered

In my current setup, I have a controller that consists of one main object containing various functions. Initially, default values for variables are set and the init() function is used to fetch data from the database. Everything on the page seems to be fu ...

Creating default values for MongoDB databases using bdhash in Express.js and mongoose asynchronously

What is the best way to set a default value (like bdhash which is async) for a field in my mongoose schema? Currently, I am only seeing a promise inside. I'm using async/await correctly, but why does it seem like I'm getting just a promise? I als ...

What seems to be the issue with the data initialization function not functioning properly within this Vue component?

In my Vue 3 component, the script code is as follows: <script> /* eslint-disable */ export default { name: "BarExample", data: dataInitialisation, methods: { updateChart, } }; function dataInitialisation() { return { c ...

Executing functions in TypeScript

I am facing an issue while trying to call a function through the click event in my template. The error message I receive is "get is not a function". Can someone help me identify where the problem lies? This is my template: <button class="btn btn-prima ...

Getting back the output in angularjs service

I have encountered two situations when using the return value of a service in AngularJS. Scenario 1: The following code works perfectly fine var SerFactProv = angular.module("SerFactProv", []) .service('ServiceDemo', function () { return { ...

Exploring advanced slot functionality in VuetifyJS through autocomplete integration with Google Places API

How can I make VuetifyJS advanced slots work seamlessly with the Google Places API? Currently, some addresses appear in the autocomplete dropdown only after clearing the input text by clicking the "x" icon in the form field. To see the problem in action, ...