简体   繁体   English

Angular JS CSS双向更新

[英]Angular JS CSS two-way updates

There are some HTML elements whose position and size are stored persistently on the backend, and are fetched when the page loads. 有些HTML元素的位置和大小永久存储在后端,并且在页面加载时获取。 These HTML elements' position and size are also user-editable (draggable and resizable), whenever such an update happens these attributes need to be saved on the backend so that they remain persistent across sessions. 这些HTML元素的位置和大小也是用户可编辑的(可拖动并可调整大小),每当发生此类更新时,这些属性都需要保存在后端,以便它们在会话之间保持不变。

There is another level of complication, in one mode called the "preview" mode, the user-editing options is turned off (ie: draggables and resizable are disabled), and the elements become responsive. 还有另一种复杂程度,在一种称为“预览”模式的模式中,用户编辑选项被关闭(即:可拖动和可调整大小被禁用),并且元素变得敏感。 The responsiveness is taken care using Angular directives. 使用Angular指令要注意响应性。 So the position and size are dynamically calculated and position. 因此位置和大小是动态计算的和位置。 Once the preview mode is turned off, the attributes are reverted back to what it was before the preview mode. 预览模式关闭后,属性将恢复为预览模式之前的状态。

I currently use ng-style when in preview mode, and in non-preview mode I use jquery's .css() method to set the styles. 当前在预览模式下使用ng-style ,在非预览模式下使用jquery的.css()方法设置样式。

However I think this is not the cleanest way to do it, How do I achieve a two-way binding of CSS such that in one state the style is populated from the model, and in the other state the model gets updated with the calculated CSS from the DOM. 但是我认为这不是最干净的方法,我如何实现CSS的双向绑定,以便在一种状态下从模型填充样式,而在另一种状态下用计算的CSS更新模型。来自DOM。

tl;dr: The CSS is either populated from a model, or saved to a model. tl; dr:从模型填充CSS或将其保存到模型。 I need a 'get css' and 'set css' method on the same HTML element, where depending on a condition only get or set is active at any given time. 我需要在同一个HTML元素上使用“ get css”和“ set css”方法,其中根据条件,在任何给定时间仅get或set处于活动状态。

Maybe if you set a container div and set jquery to made a class change 也许如果您设置了一个容器div并设置了jquery以进行类更改

 <scrpit>
   if($whateveryouwant) {
   $( ".mode" ).addClass( "preview-mode" );
    }

     else {
     $( ".mode" ).addClass( "non-preview-mode" );
    }

    </script>

Your HTML: 您的HTML:

    <div class="mode">
    ...
    </div>

您是否尝试过其他角度指令,例如ng-hide,ng-show,ng-class,... https://docs.angularjs.org/api/ng/directive

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

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