link 函数的元素参数不允许数组操作
no array operation allowed on element parameter of link function
link: function(scope, element, attrs, model)
这是 angular 指令的 link 函数。
当我查看元素对象时,它具有以下结构
[<input google-place type="text" id="existingAreas" class="form-control ng-isolate-scope" place="newProperty.address.selectedArea" country="in">]
console.log(元素) 给出这个
R[1]
0: input#existingAreas.form-control.ng-isolate-scope
length: 1
__proto__: Object[0]
看起来是一个数组。但是当我尝试对它进行 pop 之类的数组操作时,它会出错。虽然引用元素 [0] 有效。
我无法理解为什么会这样
这是一个对象,不能对其进行数组运算。如果您需要从对象中删除键,请尝试
delete element[0]
或
delete element.key
或
delete element['key']
查看 the documentation 我们可以看到:
element is the jqLite-wrapped element that this directive matches.
jQuery(以及 jqLite)return 所谓的 array-like 对象,这意味着它们具有数字索引属性和长度属性。这些看起来像数组,可以通过 call 函数与某些数组函数一起使用,但它们不是真正的数组,因此没有正常的数组方法。
要获取最后一个元素,您可以简单地执行
var last = element[element.length-1];
link: function(scope, element, attrs, model)
这是 angular 指令的 link 函数。
当我查看元素对象时,它具有以下结构
[<input google-place type="text" id="existingAreas" class="form-control ng-isolate-scope" place="newProperty.address.selectedArea" country="in">]
console.log(元素) 给出这个
R[1]
0: input#existingAreas.form-control.ng-isolate-scope
length: 1
__proto__: Object[0]
看起来是一个数组。但是当我尝试对它进行 pop 之类的数组操作时,它会出错。虽然引用元素 [0] 有效。
我无法理解为什么会这样
这是一个对象,不能对其进行数组运算。如果您需要从对象中删除键,请尝试
delete element[0]
或
delete element.key
或
delete element['key']
查看 the documentation 我们可以看到:
element is the jqLite-wrapped element that this directive matches.
jQuery(以及 jqLite)return 所谓的 array-like 对象,这意味着它们具有数字索引属性和长度属性。这些看起来像数组,可以通过 call 函数与某些数组函数一起使用,但它们不是真正的数组,因此没有正常的数组方法。
要获取最后一个元素,您可以简单地执行
var last = element[element.length-1];