简体   繁体   English

更新jQuery单击的作用域

[英]Update Scope on jquery click

在此处输入图片说明 I have a directive, that is listening on click on body, and it updates a property in an object. 我有一个指令,即监听主体上的单击,它会更新对象中的属性。 If I log the variable test of the object, it has the true value always, but if I log myObj, I see that test is updated. 如果我记录对象的变量测试,则该对象始终具有真实值,但是如果我记录myObj,则看到该测试已更新。 What to do????? 该怎么办?????

scope.myObj.test = false;
angular.element('body').unbind().click(function () {
    scope.$apply(function () {
       console.log(scope.myObj, 'here the test prop is true');
       console.log(scope.myObj.test, 'here the test prop is still false');
       if (!scope.myObj.test)
           scope.myObj.test = true;
    });
});
   console.log(scope.myObj, 'here the test prop is true');
   console.log(scope.myObj.test, 'here the test prop is still false');

This is not possible - the lines are consecutive and nothing changes between them. 这是不可能的-线是连续的,它们之间没有任何变化。

If you are using the chrome developer tools, note that if you output an object to the console, it is evaluated when you expand it. 如果使用的是chrome开发人员工具,请注意,如果将对象输出到控制台,则在扩展对象时会对其进行评估。

Instead, try this: 相反,请尝试以下操作:

   console.log(JSON.stringify(scope.myObj));
   console.log(scope.myObj.test);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM