简体   繁体   English

无法使用angular设置元素的css属性

[英]Unable to set css property of element with angular

I have an element in the dom that i get in a directive like this 我在dom中有一个元素,我在这样的指令中得到了

angular.element.find('#id-campaign-menu')

all good till i try to apply some new css to it like so 所有的好,直到我尝试应用一些新的CSS这样

var el = angular.element.find('#id-campaign-menu');
el.css({left: 400});

I get 我明白了

Uncaught TypeError: el.css is not a function(…)

Angular jQLite object doesn't lookup through custom element 's and custom selector 's. Angular jQLite对象不会通过自定义element和自定义selector查找。 It does look up for nodes / DOM only like button , input , div , etc. 它只查找nodes / DOMbuttoninputdiv等。

If you loaded jQuery before Angular then jQLite API gets overrided by jQuery API. 如果您在Angular之前加载了jQuery,那么jQLite API将被jQuery API覆盖。 So you can do selector based query over DOM. 所以你可以在DOM上做基于选择器的查询。

You could change your query to below for make it working with jQLite . 您可以将查询更改为以下,以使其与jQLite一起使用。

var campaignMenu = angular.element(document.getElementById('id-campaign-menu');
campaignMenu.css({left: 400});

Here's the list of functions supported by Angular's jqLite . 这是Angular的jqLit​​e支持的函数列表

Note that for .css() : 请注意,对于.css()

css() - Only retrieves inline-styles, does not call getComputedStyle() . css() - 只检索内联样式,不调用getComputedStyle() As a setter, does not convert numbers to strings or append 'px', and also does not have automatic property prefixing. 作为一个setter,不会将数字转换为字符串或附加'px',也没有自动属性前缀。

So to support .css() calls that mutate the DOM, import jQuery before Angular, or use vanilla JS document selectors like document.getElementById . 因此,要支持改变DOM的.css()调用,在Angular之前导入jQuery,或者使用像document.getElementById这样的vanilla JS文档选择器。

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

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