Break on variable change in Chrome using AngularJS

跟風遠走 提交于 2019-12-11 04:08:51

问题


Similar to this question, I would like to break on a variable change in Chrome. However, I'm using Angular, so the variable I would like to break on is only defined in the HTML. Its essentially something like this -

<div ng-click="show = !show">...<div>
<div ng-class="{expanded : show}">...

With no code in the controller. So how can I get Chrome to pause when show is changed? I know that I can change the code to a function call that wraps show and then put a breakpoint there, but its an inefficient use of time and I would like to know if there is a way to directly break on variable change.


回答1:


Well, you can achive the desired feature via console:

  1. First get access to the scope, on which show property is defined. Here is an explanation of how you can do this.
  2. Then you may observe changes of this scope object via Chrome's Object.observe.

To simplify this process you may define global function like this one:

function breakOn(property, object) {
    Object.observe(object, function (changes) {
      changes.forEach(function (change) {
        if (change.name === property) {
          console.log("Property " + property + " changed");
          console.log(change);
          debugger;
        }
      });
    });
  }

And then after step 1 type in the console: breakOn('show', $scope);



来源:https://stackoverflow.com/questions/26302096/break-on-variable-change-in-chrome-using-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!