[英]How to select element inside Angular 1.5 component?
For component , I can no longer refer to Elem
or Attr
like I used to in its father -- directive, by taking them as arguments inside link
or post
. 对于component ,我不再像以前在其父-指令中那样引用
Elem
或Attr
,而是将它们作为link
或post
内部的参数。
What is the best way to select element like I used to in directive? 像我以前在指令中那样选择元素的最佳方法是什么? Did it change the way how it should be done?
它改变了应该怎么做的方式吗?
Why is it not documented in the component document for the Angular 1.5x? 为什么在Angular 1.5x的组件文档中没有记录?
You could do the same thing inside component controller by injecting $element
dependency. 您可以通过注入
$element
依赖项在组件控制器内部执行相同的操作。 But on controller load $element
is not compiled DOM. 但是在控制器加载时,
$element
不是编译的DOM。
For such case you could use Angular 1.5 component life cycle, like there we have $postLink()
which will work as same as like postLink
/ link
function of Angular 1 directive. 在这种情况下,您可以使用Angular 1.5组件生命周期,就像我们有
$postLink()
,它的工作原理与Angular 1指令的postLink
/ link
函数相同。
For accessing attribute you inject $attrs
service inside controller. 为了访问属性,您需要在控制器内部注入
$attrs
服务。
Component 零件
myMod.component('myComponent', {
template: '<h1>Home</h1>',
controller: function($element) {
this.test = 'hello world';
this.$postLink = function(){
//here you have compiled DOM
//you can play with element here.
console.log("Post LInk DOM", $element);
}
console.log("Initial DOM", $element);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.