Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用addEventListener从触发属性时角度组件不重新渲染_Javascript_Angularjs - Fatal编程技术网

Javascript 使用addEventListener从触发属性时角度组件不重新渲染

Javascript 使用addEventListener从触发属性时角度组件不重新渲染,javascript,angularjs,Javascript,Angularjs,我有一个角度组件(im使用v1.5.6),它监听设备方向,然后显示一条消息,如果用户正在使用平板电脑并已转到纵向模式(检查是否正常)。我的模板是: <div ng-show="$crtl.isTablet" class="orientation-warning-message tablet-warning-message"> <p>We think you are accessing this app on a tablet - you must use the

我有一个角度组件(im使用v1.5.6),它监听设备方向,然后显示一条消息,如果用户正在使用平板电脑并已转到纵向模式(检查是否正常)。我的模板是:

<div ng-show="$crtl.isTablet" class="orientation-warning-message tablet-warning-message">
    <p>We think you are accessing this app on a tablet - you must use the app in landscape mode to continue (turn on side)</p>
</div> 

但该消息不会显示,因为在
addEventListener
回调中设置
isTablet
似乎不会重新呈现组件。如果我在
addEventListener
回调之外将
isTablet
设置为
true
,则它可以工作。是否需要手动触发组件重新渲染?

是否尝试添加$scope.$apply()…例如:

因为无法看到外部事件(如SetTimeOut()),所以必须强制它刷新范围

angular
  .module('myApp')
  .component('checkDevice', {
    templateUrl: 'directives/checkDevice/checkDevice.tpl.html',
    controller: function() {
      var self = this;

      this.isTablet = false;

      window.addEventListener("orientationchange", function() {

        // do some checks here and if a tablet and on portrait mode then:
        self.isTablet = true;

      }, false);

    }
});
angular
  .module('myApp')
  .component('checkDevice', {
    templateUrl: 'directives/checkDevice/checkDevice.tpl.html',
    controller: function(scope) {
      var self = this;

      this.isTablet = false;

      window.addEventListener("orientationchange", function() {


        // do some checks here and if a tablet and on portrait mode then:
    scope.$apply(function(){     
       self.isTablet = true;
    });
      }, false);

    }
});