Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

This has come up while I was trying to change the back color of a li whenever the text changes. From another postpost I was directed to, I found a method of doing this which looks ideal, and have added to my plunker example.

So, the main bit of code here is in this directive...

 .directive('animateOnChange', function ($timeout) {       
   return function (scope, element, attr) {
     
     scope.$watch(attr.animateOnChange, function (nv, ov) {
    if (nv != ov) { // change to true and this works
      element.addClass('changed');
      $timeout(function() {
        element.removeClass('changed');
      }, 500); // Could be enhanced to take duration as a parameter
    }
  });
};
})

When you click the button, a property is updated, but we get no color change, as the values of nv and ov at the line if (nv != ov) are always the same (if you replace the nv != ov with just true we can see the color change, which also then occurs at initialisation, which I don't really want.

So the question is why would my old value and new value (ov, nv) always be the same?

Any help greatly appreciated.

This has come up while I was trying to change the back color of a li whenever the text changes. From another post I was directed to, I found a method of doing this which looks ideal, and have added to my plunker example.

So, the main bit of code here is in this directive...

 .directive('animateOnChange', function ($timeout) {       
   return function (scope, element, attr) {
     
     scope.$watch(attr.animateOnChange, function (nv, ov) {
    if (nv != ov) { // change to true and this works
      element.addClass('changed');
      $timeout(function() {
        element.removeClass('changed');
      }, 500); // Could be enhanced to take duration as a parameter
    }
  });
};
})

When you click the button, a property is updated, but we get no color change, as the values of nv and ov at the line if (nv != ov) are always the same (if you replace the nv != ov with just true we can see the color change, which also then occurs at initialisation, which I don't really want.

So the question is why would my old value and new value (ov, nv) always be the same?

Any help greatly appreciated.

This has come up while I was trying to change the back color of a li whenever the text changes. From another post I was directed to, I found a method of doing this which looks ideal, and have added to my plunker example.

So, the main bit of code here is in this directive...

 .directive('animateOnChange', function ($timeout) {       
   return function (scope, element, attr) {
     
     scope.$watch(attr.animateOnChange, function (nv, ov) {
    if (nv != ov) { // change to true and this works
      element.addClass('changed');
      $timeout(function() {
        element.removeClass('changed');
      }, 500); // Could be enhanced to take duration as a parameter
    }
  });
};
})

When you click the button, a property is updated, but we get no color change, as the values of nv and ov at the line if (nv != ov) are always the same (if you replace the nv != ov with just true we can see the color change, which also then occurs at initialisation, which I don't really want.

So the question is why would my old value and new value (ov, nv) always be the same?

Any help greatly appreciated.

Source Link
peterc
  • 8k
  • 14
  • 85
  • 166

Angular $watch old value always equals new value when used to watch an li

This has come up while I was trying to change the back color of a li whenever the text changes. From another post I was directed to, I found a method of doing this which looks ideal, and have added to my plunker example.

So, the main bit of code here is in this directive...

 .directive('animateOnChange', function ($timeout) {       
   return function (scope, element, attr) {
     
     scope.$watch(attr.animateOnChange, function (nv, ov) {
    if (nv != ov) { // change to true and this works
      element.addClass('changed');
      $timeout(function() {
        element.removeClass('changed');
      }, 500); // Could be enhanced to take duration as a parameter
    }
  });
};
})

When you click the button, a property is updated, but we get no color change, as the values of nv and ov at the line if (nv != ov) are always the same (if you replace the nv != ov with just true we can see the color change, which also then occurs at initialisation, which I don't really want.

So the question is why would my old value and new value (ov, nv) always be the same?

Any help greatly appreciated.