Javascript 使用addEventListener从触发属性时角度组件不重新渲染
我有一个角度组件(im使用v1.5.6),它监听设备方向,然后显示一条消息,如果用户正在使用平板电脑并已转到纵向模式(检查是否正常)。我的模板是: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
<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);
}
});