Angular - connecting a function directly

Is there any potential performance impact of directly binding a function in directives like ng-show in AngularJS?

<div ng-show="myVm.isVisible()">
....
</div>

// controller snippet (exposed through controllerAs syntax)
function myCtrl (myService, authService) {
  this.isVisible = function isVisible () {
    return (myService.state === 'foo' && authService.isAuthorised);
  }
}

This approach allows for concealing complex logic in the template and moving it into the testable controller or service. However, some developers express concerns about binding functions in directives such as ng-show and ng-if.

Answer №1

Essentially, the speed of your function plays a major role here. Angular will repeatedly evaluate your function on every digest cycle and compare it with its previous result. This constant comparison can slow down performance compared to simply checking a basic variable. If the efficiency of this function is crucial (for example, if it takes a significant amount of time to compute), consider storing the output in a variable. Then, in your template, check that variable instead and update it as necessary.

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

Tips for determining the duration between the Monday of last week and the Sunday of last week

Can anyone help me figure out how to retrieve the dates from last week's Monday to last week's Sunday using JavaScript? I've searched through various sources with no luck. Hoping someone here can provide some guidance. ...

Utilize the same controller and view for both searching and non-searching functionality within Angular

Currently, I am developing an Angular application that consists of a single controller and view. I am facing the challenge of wanting to display the same view with or without a search field, based on user preference. My idea is to pass an extra parameter ...

How can I trigger a success event in JavaScript after ASP.NET form validation?

Is there a way for me to be notified when my ASP.NET form validation is successful so I can subscribe to that event? Here's the situation: When a user clicks a button on a payment form, I want to display a message saying "processing," but only if the ...

What could be causing issues with angularjs ng-include not functioning properly in Chrome at times?

<html ng-app="myApp1"> <head> <title>NG-Include</title> <script src="angular.js"></script> <script src="filter.js"></script> </head> <body> <div ng-controller="myController"> ...

Parsing JSON stored in local storage and converting it to a Fabric JS object - JSON generated from form inputs

Currently, I am facing some challenges while using Fabric js for a project that I am working on. My main goal is to create a form where users can input details, which will then be converted into a JSON object and stored locally. After submitting the form, ...

Empty Data Table Search After Refresh While Filter Remains Active

After testing the data tables library using their example code in a jsfiddle, it seems like there might be a bug. To recreate the issue, follow these steps: Open the JS Fiddle link https://jsfiddle.net/t4rphnuc/ Click "Run" In any footer search box, fil ...

Dependency injection of an Angular app factory toaster is causing the application to malfunction

I am currently working on an Angular application that utilizes Firebase as its backend. My goal is to inject 'toaster' as a dependency within my authorization app factory. Below is the initial setup of the app.factory: app.factory('principa ...

The invocation of $.ajax does not work as a function

I'm encountering an issue when running npm start and trying to access the browser, I keep getting this error message in the stack trace. Error: $.ajax is not functioning properly at findLocation (G:\CodeBase\ExpressApp\routes\ind ...

Utilizing a switch statement for form validation

Currently, I am in the process of creating a form validation that involves two conditions for validation. I'm considering using a combination of switch case and if else statements. Would this be an appropriate approach or is it generally discouraged? ...

What is the best way to exchange the chosen selection within 2 select elements?

I have been trying to swap the cities in my select fields using two select elements, but for some reason, it is not working when the button is clicked: https://i.sstatic.net/mHg4Y.jpg <div class="select-wrapper"> <select class="airport-selec ...

Retrieve the values of two inputs from outside of the event

How can I access the values of two input fields in order to perform calculations as soon as their values are updated? <script> // Accessing the values of the two input fields for immediate calculation based on user input const heightInputEl = docum ...

Exploring the concept of jQuery handler functions

Within my code, I have the following: <script type="text/javascript"> $(document).ready(function (e) { $('#EventCreate').click(function (e) { location.href = '@Url.Action("Create", "AEvents")'; }); ...

Experimenting with Chai in JavaScript to test an incorrect argument

Background: I recently delved into JavaScript and have been experimenting with it. It's possible that my question may sound silly, but I am eager to learn. I have developed a function called `getDayOfTheWeekFromDate` which returns the day of the week ...

Is there a way to differentiate the color of an active link from an inactive one, so users can easily identify which link they are currently viewing?

How can I customize the color of text for the active page on my website's sidebar? For example, I have two hyperlinks, one for HOME and one for ABOUT. When a user clicks on the HOME link, I want the text color to change to green while the ABOUT link r ...

Validating multiple fields that are added dynamically using jQuery

I am facing an issue with form validation using jQuery. The problem is that when one field is valid, the form gets submitted automatically. Here is a link to my code: http://jsfiddle.net/cvL0ymu7/. How can I ensure that all fields are validated before subm ...

The token was retrieved as [object Object] in the Ionic framework

Utilizing import { Stripe } from '@ionic-native/stripe' to generate a card token, but encountering the issue where the createCardToken() function is returning an object Object. Upon calling this.stripe.createCardToken(card), the promise should r ...

Prevent the opening of tabs in selenium using Node.js

Currently, I am using the selenium webdriver for node.js and loading an extension. The loading of the extension goes smoothly; however, when I run my project, it directs to the desired page but immediately the extension opens a new tab with a message (Than ...

Learn the process of utilizing ng-class within ng-repeat to designate a specific class for individual iterative div elements

**On my homepage, I am displaying data retrieved from a MySQL database in cards. However, all the tags have the same color i.e. class="tag tag-pill tag-danger". To resolve this issue, I am using ng-class={{hos.class}} where class is a ...

Vue.js: Awaiting Firebase to finish loading

I'm facing an issue with integrating Firebase into a Vue JS component. It seems like the firebase object loads after my component is created. Is there a way to ensure that Firebase is fully loaded before running any JavaScript code? For example, I w ...

Validating Firebase data for null values

Hey there, I'm currently working on a simple coding project but seems to be encountering some roadblocks. The main objective of the code is to determine if a username exists in the system or not. Here's a snippet of the data structure and codes ...