我正在努力加快使用 1.5 个角度分量的速度。我一直在关注 todd Motto 的视频,以了解组件以及 Angular 的文档 https://docs.angularjs.org/guide/component。
在这一点上,组件似乎正在取代使用控制器的指令,但在我们的 1.5 代码中,我们仍然会使用指令来进行 dom 操作。
$element, $attrs 在组件控制器中的用途是什么?这些似乎可用于操纵。这是文档中 plunker 的链接。我知道他们没有使用 $element,但这是我正在阅读的示例。http://plnkr.co/edit/Ycjh1mb2IUuUAK4arUxe?p=preview
但是在这样的代码中......
angular
.module('app', [])
.component('parentComponent', {
transclude: true,
template: `
<div ng-transclude></div>
`,
controller: function () {
this.foo = function () {
return 'Foo from parent!';
};
this.statement = function() {
return "Little comes from this code";
}
}
})
.component('childComponent', {
require: {
parent: '^parentComponent'
},
controller: function () {
this.$onInit = function () {
this.state = this.parent.foo();
this.notice = this.parent.statement();
};
},
template: `
<div>
Component! {{ $ctrl.state }}
More component {{$ctrl.notice}}
</div>
`
})
如果我们不操作 dom,那么 $element 有什么用?