简体   繁体   English

ng-hide没有动态更新

[英]ng-hide is not getting updated dynamically

I have below div element with nghide 我有下面的div元素与nghide

 <div ng-hide="showdiv" class="btnshowall"> 
    <a class="button button-block round outline"
       style="background: transparent !important;" >
      Show All
    </a>
 </div>

and controller as below 和控制器如下

.controller('mapCtrl', ['$scope', '$stateParams','User','$cordovaGeolocation','geoFireFac','GoogleMapFac','ConnectivityMonitor','PhysioFac','User',
function ($scope, $stateParams,User,$cordovaGeolocation,geoFireFac,GoogleMapFac,ConnectivityMonitor,PhysioFac,User) {

    console.log('called mapctrl');  
    GoogleMapFac.setUserLoc($scope.map);
    $scope.showdiv = User.getShowDiv();


}])

and User service as 和用户服务

.service('User', ['ToastFac',function(ToastFac){
    return {
         showDiv : false,
        changeShowDiv : function(){
            console.log('in changeShowDiv before change '+this.showDiv);
            this.showDiv = !this.showDiv;
            console.log('in changeShowDiv after change '+this.showDiv);

        },

        getShowDiv : function(){
            return this.showDiv;
        }

I am invoking User.changeShowDiv() from google map's marker click event like below 我从谷歌地图的标记点击事件调用User.changeShowDiv(),如下所示

google.maps.event.addListener(marker, 'click', function () {
      alert('store id '+marker.get('store_id'));
      if(User.showDiv){
          console.log('in if');
          User.changeShowDiv();
          console.log('User.showDiv '+User.showDiv);
      }
      else{
          console.log('in else');
          User.changeShowDiv();
          console.log('User.showDiv '+User.showDiv);
      }

});

logs are coming as expected 日志按预期进行

in else
services.js:123 in changeShowDiv before change false
services.js:125 in changeShowDiv after change true
services.js:218 User.showDiv true
services.js:211 in if
services.js:123 in changeShowDiv before change true
services.js:125 in changeShowDiv after change false
services.js:213 User.showDiv false
services.js:216 in else
services.js:123 in changeShowDiv before change false
services.js:125 in changeShowDiv after change true
services.js:218 User.showDiv true

By default, as User.showDiv variable is false, showAll button is visible. 默认情况下,当User.showDiv变量为false时,showAll按钮可见。 But button is not hiding & coming by marker click events. 但是按钮不是通过标记点击事件隐藏和来的。

Could someone guide me what I am missing. 有人可以指导我,我错过了什么。

Events that come from outside the AngularJS framework need to be brought into the AngularJS framework with $apply : 来自AngularJS框架之外的事件需要使用$ apply引入AngularJS框架:

google.maps.event.addListener(marker, 'click', function () {
      alert('store id '+marker.get('store_id'));
      if(User.showDiv){
          console.log('in if');
          User.changeShowDiv();
          console.log('User.showDiv '+User.showDiv);
      }
      else{
          console.log('in else');
          User.changeShowDiv();
          console.log('User.showDiv '+User.showDiv);
      }
      //IMPORTANT
      $scope.$apply();    
});

AngularJS modifies the normal JavaScript flow by providing its own event processing loop. AngularJS通过提供自己的事件处理循环来修改正常的JavaScript流。 This splits the JavaScript into classical and AngularJS execution context. 这将JavaScript拆分为经典和AngularJS执行上下文。 Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc... You can also use $apply() to enter the AngularJS execution context from JavaScript. 只有在AngularJS执行上下文中应用的操作才能受益于AngularJS数据绑定,异常处理,属性监视等...您还可以使用$apply()从JavaScript输入AngularJS执行上下文。 Keep in mind that in most places (controllers, services) $apply has already been called for you by the directive which is handling the event. 请记住,在大多数地方(控制器,服务),已经通过处理事件的指令为您调用了$apply An explicit call to $apply is needed only when implementing custom event callbacks, or when working with third-party library callbacks. 仅在实现自定义事件回调或使用第三方库回调时才需要显式调用$apply

FF

— AngularJS Developer Guide - Integration with the browser event loop - AngularJS开发人员指南 - 与浏览器事件循环集成


ALSO

Be sure to fix the ng-hide and the controller: 务必修复ng-hide和控制器:

<div ng-hide="showdiv()" class="btnshowall">
$scope.showdiv = function() {
    return User.getShowDiv();
};

In the above code, the ng-hide directive will execute the showdiv() function on each digest cycle and update the visibility of the element accordingly. 在上面的代码中, ng-hide指令将在每个摘要周期上执行showdiv()函数,并相应地更新元素的可见性。

You're retrieving value from User.getShowDiv method once only. 您只从User.getShowDiv方法中检索一次值。 But when it gets change your are not updating showdiv scope variable. 但是当它发生变化时,你不会更新showdiv范围变量。 To update value each time you can directly bind the reference of User.getShowDiv method to showdiv scope variable like below 每次可以直接将User.getShowDiv方法的引用绑定到showdiv范围变量时更新值,如下所示

$scope.showdiv = User.getShowDiv; 

There after call showdiv method on HTML, which will eventually evaluate value on each digest cycle unlike other bindings . 在HTML上调用showdiv方法之后,最终将评估每个摘要周期的值,这与其他bindings不同。

ng-hide="showdiv()"

Even above would not solve your problem. 即便如此也无法解决您的问题。 Basically you're updating some variable from outside context Angular which is click event. 基本上你是从外部上下文Angular更新一些变量,它是click事件。 So you have to run digest cycle manually right after updating value from click event listener ran. 所以你必须在click事件监听器运行后更新值后立即手动运行摘要周期。 Just do use $timeout(angular.noop) to fire digest cycle safely. 只需使用$timeout(angular.noop)安全地$timeout(angular.noop)摘要周期。

google.maps.event.addListener(marker, 'click', function () {
      alert('store id '+marker.get('store_id'));
      if(User.showDiv){
          //Code here
      }
      else{
          //Code here
      }
      //manually triggering digest loop to make binding in sync
      $timeout(angular.noop); //It will run new digest cycle.
});

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

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