繁体   English   中英

ng-hide没有动态更新

[英]ng-hide is not getting updated dynamically

我有下面的div元素与nghide

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

和控制器如下

.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();


}])

和用户服务

.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;
        }

我从谷歌地图的标记点击事件调用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);
      }

});

日志按预期进行

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

默认情况下,当User.showDiv变量为false时,showAll按钮可见。 但是按钮不是通过标记点击事件隐藏和来的。

有人可以指导我,我错过了什么。

来自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通过提供自己的事件处理循环来修改正常的JavaScript流。 这将JavaScript拆分为经典和AngularJS执行上下文。 只有在AngularJS执行上下文中应用的操作才能受益于AngularJS数据绑定,异常处理,属性监视等...您还可以使用$apply()从JavaScript输入AngularJS执行上下文。 请记住,在大多数地方(控制器,服务),已经通过处理事件的指令为您调用了$apply 仅在实现自定义事件回调或使用第三方库回调时才需要显式调用$apply

FF

- AngularJS开发人员指南 - 与浏览器事件循环集成


务必修复ng-hide和控制器:

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

在上面的代码中, ng-hide指令将在每个摘要周期上执行showdiv()函数,并相应地更新元素的可见性。

您只从User.getShowDiv方法中检索一次值。 但是当它发生变化时,你不会更新showdiv范围变量。 每次可以直接将User.getShowDiv方法的引用绑定到showdiv范围变量时更新值,如下所示

$scope.showdiv = User.getShowDiv; 

在HTML上调用showdiv方法之后,最终将评估每个摘要周期的值,这与其他bindings不同。

ng-hide="showdiv()"

即便如此也无法解决您的问题。 基本上你是从外部上下文Angular更新一些变量,它是click事件。 所以你必须在click事件监听器运行后更新值后立即手动运行摘要周期。 只需使用$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