在AngularJS运行时设置HTML元素的宽度 [英] Set Width of HTML element at runtime in AngularJS

查看:393
本文介绍了在AngularJS运行时设置HTML元素的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时从我的AngularJS控制器设置一个HTML元素的宽度。我控制我的指令中规定。我知道我需要设置的第一个 UL 指令中元素的宽度。目前,我有以下几点:

I need to set the width of an HTML element at runtime from my controller in AngularJS. My controller is defined within my directive. I know that I need to set the width of the first ul element within the directive. Currently, I have the following:

var list = $element.find('ul');
$element(list[0]).css('width', '300px'));

然而,当我尝试,我得到一个错误,指出:

However, when I try this, I get an error that says:

TypeError: object is not a function

我是什么做错了吗?我如何在AngularJS设置一个HTML元素的宽度在运行时?

What am I doing wrong? How do I set the width of an HTML element at runtime in AngularJS?

感谢您!

推荐答案

您不需要JQuery的做你想做的,AngularJS有 jqlite 。由于在 AngularJS开发指南中提到的指令

You don't need JQuery to do what you want, AngularJS has jqlite. As mentioned in the AngularJS Developer's Guide for directives:

元素 jqLit​​e -wrapped元素,这条指令匹配。

element is the jqLite-wrapped element that this directive matches.

因此​​,它应该能够做到大部分的jQuery是能够的特性,在 angular.element()文档

So it should be able to do most of the features that jQuery is capable of, as defined in the angular.element() documentation.

什么文档没有限定,是查找元素jqlite版本返回HTML元素的数组。所以,你必须使用 angular.element()要使用jqlite功能数组里面操纵各个元素。因此,code通常应该是这样的:

What the documentation fails to define, is that the jqlite version for finding elements returns an array of HTML elements. So you have to use angular.element() on each element that you want to manipulate inside that array using the jqlite functionalities. So the code should generally look like this:

演示

DEMO

   var list = elem.find('ul');
   angular.element(list[0]).css('width', '300px');

这篇关于在AngularJS运行时设置HTML元素的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆