小编vs7*_*vs7的帖子

根据屏幕分辨率AngularJS更改指令的templateUrl

我需要根据屏幕分辨率更改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'
    }
});
Run Code Online (Sandbox Code Playgroud)

在这里,我尝试使用此代码

 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
    }
});
Run Code Online (Sandbox Code Playgroud)

此代码块正在运行,它根据分辨率加载移动和桌面页面但是当我调整页面大小时它保持不变...

例如,如果我在最小化窗口(480px)中打开浏览器并将其最大化为1366px,则templateUrl保持与"/templates/browse-content-mobile.html"相同,它必须是"/templates/browse-content.html"

javascript jquery angularjs

11
推荐指数
2
解决办法
7889
查看次数

标签 统计

angularjs ×1

javascript ×1

jquery ×1