我需要根据屏幕分辨率更改templateURL,例如,如果我的屏幕宽度小于768px,则必须加载“templates/browse-content-mobile.html”,如果大于768px,则必须加载“templates/browse-content.html”。
当前使用的代码。
app.directive('browseContent', function() {
return {
restrict: 'E',
templateUrl: template_url + '/templates/browse-content.html'
}
});
我在尝试使用这段代码
app.directive('browseContent', function() {
screen_width = window.innerWidth;
if (screen_width < 768) {
load_tempalte = template_url + '/templates/browse-content-mobile.html';
} else if (screen_width >= 768) {
load_tempalte = template_url + '/templates/browse-content.html';
}
return {
restrict: 'E',
templateUrl: load_tempalte
}
});
这段代码块是有效的,它会根据移动设备和桌面设备的分辨率来加载相应的页面。但是当我调整页面大小时,它仍然保持不变...
例如,如果我将浏览器在最小化窗口(480像素)中打开,然后将其最大化到1366像素,templateUrl仍然保持为“/templates/browse-content-mobile.html”,而应该是“/templates/browse-content.html”