Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8990651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:32:45+00:00 2026-06-15T22:32:45+00:00

fiddle I have a list, and I’d like to be notified whenever it changes.

  • 0

fiddle

I have a list, and I’d like to be notified whenever it changes. A simple $watch expression isn’t working, which I’m guessing is because angular is checking for referential equality, not structural equality.

<html ng-app>
    <head>
    </head>
    <body ng-controller="Root">
        <h1>Base Angular Fiddle</h1>
        Times changed: {{timesChanged}}
        <ul>
            <li ng-repeat="name in names">{{name}}</li>
            <li><button ng-click="names.push('another name')">Add</button></li>
        </ul>
    </body>
</html>
​

​function Root($scope) {
    $scope.timesChanged = 0;
    $scope.names = ['foo', 'bar', 'baz'];
    $scope.$watch('names', function() {
        $scope.timesChanged++;
    });
}​

What I’m hoping will happen is that the callback for 'names' is called each time that I call names.push(). Is there a workaround that’s recommended for this? Or am I just not using $watch correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:32:46+00:00Added an answer on June 15, 2026 at 10:32 pm

    You are right, angular checks for reference by default, for perfomance reasons. The usage of reference vs equality depends on the value of the $watch function’s third argument, as you can see in the docs. Just set it to true (it is false by default) and your fiddle will work – here it is, working.

    the change:

    $scope.$watch('names', function() {
            $scope.timesChanged++;
        }, true);
    

    EDIT – Performance implications:

    Comparing two arrays/objects can obviously be very inefficient (hence the check by reference angular makes by default), depending on the size and complexity of your variables. There is an alternate method – watching the length of the array. This has obvious limitations – changing one element no longer triggers the $watch function – but can be immensely faster. Here is an updated fiddle!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the fiddle http://jsfiddle.net/srinivasanvee/yYEwJ/2/ I have a list which has category, product, quantity
I have this fiddle : http://jsfiddle.net/XjeCf/1/ that works like I want and this one
I have a list with child elements like : <ul> <li>.. </li> <li> <ul>
I have unordered list which has bulleted text within. I need to indent the
I have created a fiddle with the following situation and I'd like an idea
I have a list which is inside a list item: <ul id=selected-list class=droptrue sort-drop>
I have a unordered list with links and sublinks. I'd like to prepend a
I made this fiddle to show my problem. I have a list and I
I have a fiddle for this issue which has been annoying me for a
I have created a fiddle: http://jsfiddle.net/pQZ8f/ I want to have both the list items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.