最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to make angularJS $watch to detect changes in style? - Stack Overflow

matteradmin4PV0评论

I am having a rich text box with all the style controls.

When I enter a new text in it - it saves.

When I make any changes in the content (text) along with some styles like color highlighting and bold text - it saves the changed text and styles.

But, when I just make style changes without any content change - it won't save those style changes.

I am using $watch to pare new value and old value.

How to make it work even for style changes?

I am having a rich text box with all the style controls.

When I enter a new text in it - it saves.

When I make any changes in the content (text) along with some styles like color highlighting and bold text - it saves the changed text and styles.

But, when I just make style changes without any content change - it won't save those style changes.

I am using $watch to pare new value and old value.

How to make it work even for style changes?

Share Improve this question edited Mar 5, 2015 at 13:56 R3tep 12.9k10 gold badges51 silver badges76 bronze badges asked Mar 5, 2015 at 13:52 Pradvaar cruzPradvaar cruz 2911 gold badge9 silver badges24 bronze badges 1
  • 1 Can you attach your code please? – mmmmmpie Commented Mar 5, 2015 at 13:54
Add a ment  | 

1 Answer 1

Reset to default 10
angular.module("app",[]).directive('spyStyle', [function () {

    return {

        link: function (scope, element, attrs) {

            scope.$watch(function () {
                return element.css(attrs['spyAttribute']);
            },  styleChangedCallBack,
            true);

            function styleChangedCallBack(newValue, oldValue) {
                if (newValue !== oldValue) {
                    // do something interesting here
                }
            }

        }
    };

}]);

In your HTML:

<div spy-style spy-attribute="height" >

In case you want to execute custom functions outside the directive:

<div spy-style="functionNameToExecute" spy-attribute="height" >

In your Directive code make this change:

angular.module("app",[]).directive('spyStyle', [function () {

    return {

        link: function (scope, element, attrs) {

            scope.$watch(function () {
                return element.css(attrs['spyAttribute']);
            },  styleChangedCallBack,
            true);

            function styleChangedCallBack(newValue, oldValue) {
                if (newValue !== oldValue) {
                    // do something interesting here
                    // invoking callback function:
                    scope[attrs['spyStyle']](newValue);
                }
            }

        }
    };

}]);
Post a comment

comment list (0)

  1. No comments so far