简体   繁体   English

Angular JS的动态和响应式类名

[英]Dynamic and responsive class names with Angular JS

I have a set of images that are brought back from the server through an Angular scope and I'd like to set a unique class name for each div wrapper. 我有一组通过Angular范围从服务器返回的图像,我想为每个div包装器设置一个唯一的类名。 The css needs to be responsive as well where the background-image changes based on the page layout. 背景图像根据页面布局而变化的地方,css也需要响应。 For instance, if the page width is below 768px, the background-image uses a smaller image size ( ImageMediumUrl ) otherwise it will use the big size ( ImageUrl ). 例如,如果页面宽度小于768px,则背景图像使用较小的图像大小( ImageMediumUrl ),否则将使用较大的图像大小( ImageUrl )。

My code for constructing the html is as follows: 我用于构造html的代码如下:

<div class="item" data-ng-repeat="image in carousel_images">
    <img border="0" src="/Content/img/blank.gif" ng-style="{'background-image':'url({{image.ImageUrl}})'}" />
</div>

Currently, the background-image is set to the big size as you can tell from the code above. 当前,如您从上面的代码可以看出的,背景图像设置为较大的尺寸。

How can I dynamically create a set of responsive class names (media queries in Angular?) for every div wrapper? 如何为每个div包装器动态创建一组响应类名(Angular中的媒体查询?)?

Perhaps you need to create a directive that detects window resize and then rebuild the markup depending on the current size / window state. 也许您需要创建一个指令来检测窗口的大小,然后根据当前的大小/窗口状态重新构建标记。

Here's an example of a directive that does this and sets the "display mode" on the bound attribute. 这是一个指令的示例,该指令执行此操作并在bound属性上设置“显示模式”。

http://www.dnasir.com/2013/10/09/display-mode-detection-for-responsive-websites-using-angularjs/ http://www.dnasir.com/2013/10/09/display-mode-detection-for-sensitive-websites-using-angularjs/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM