AngularJS中ng-include随屏幕宽度改变实例

本文详细介绍了如何在Angular应用中利用matchmedia-ng插件检测设备屏幕尺寸,并根据不同尺寸调整布局和加载不同模板。通过实例演示了如何在控制器中监听屏幕尺寸变化并动态更新视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用到了matchmedia-ng这个插件(https://github.com/AnalogJ/matchmedia-ng),参考了http://analogj.github.io/matchmedia-ng/的源代码。

html文件:

 <div>
            <div id="locationdiv" ng-include="location.url">
            </div>
</div> 

controller中:
$scope.location = {}
matchmedia.onLandscape(function(mediaQueryList){
		if(mediaQueryList.matches){
			$scope.location.url = "templates/WidePriview.html";
		}
	});
	matchmedia.onPortrait(function(mediaQueryList){
		if(mediaQueryList.matches){
			$scope.location.url = "templates/NarrowPriview.html";
		}
	});

定义$scope.location = {} 是参考了这个http://stackoverflow.com/questions/19045125/angularjs-nginclude-dynamically-changing-its-location

上面那个地址里提供了解释为什么要这么做,大意就是没有点,就是纯复制变量,后续的变化不会反映在拷贝上;有了点,就是引用object,就可以反映后续变化。

I think this needs a short explanation. This has actually to do with how JavaScript will copy things. Without the dot, we are referencing a scalar variable. If JavaScript makes a copy of that, it is just that, a copy. Any changes made to the copy will not reflect on the original. On the other hand: If there is a dot, then the "copy" is not of a scalar variable but an object (tpl in the answer). And a object is "copied" by reference, thus the copied tpl will still point to the original object, thus tpl.whatever will change tpl.whatever on the "original" as well. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值