我如何告诉 AngularJS“刷新"? [英] How can I tell AngularJS to "refresh"
问题描述
我有一个在我的自定义指令范围之外发生的点击事件,所以我没有使用ng-click"属性,而是使用 jQuery.click() 侦听器并在我的范围内调用一个函数,如下所示:
$('html').click(function(e) {范围.close(););
close() 是一个简单的函数,如下所示:
scope.close = function() {scope.isOpen = false;}
在我看来,我有一个ng-show"绑定到 isOpen 的元素,如下所示:
我的 Div
调试时,我发现正在调用 close(),正在将 isOpen 更新为 false,但 AngularJS 视图没有更新.有没有办法可以手动告诉 Angular 更新视图?或者是否有更角度"的方法来解决我没有看到的这个问题?
解决方案是调用...
$scope.$apply();
...在我的 jQuery 事件回调中.
I have a click event that happens outside the scope of my custom directive, so instead of using the "ng-click" attribute, I am using a jQuery.click() listener and calling a function inside my scope like so:
$('html').click(function(e) {
scope.close();
);
close() is a simple function that looks like this:
scope.close = function() {
scope.isOpen = false;
}
In my view, I have an element with "ng-show" bound to isOpen like this:
<div ng-show="isOpen">My Div</div>
When debugging, I am finding that close() is being called, isOpen is being updated to false, but the AngularJS view is not updating. Is there a way I can manually tell Angular to update the view? Or is there a more "Angular" approach to solving this problem that I am not seeing?
The solution was to call...
$scope.$apply();
...in my jQuery event callback.
这篇关于我如何告诉 AngularJS“刷新"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!