I am working on getting up to speed with 1.5 angular components. I have been following todd Motto's videos to get a start on components along with angular's documentation https://docs.angularjs.org/guide/component.
At this point it seems components are taking the place of directives that use controllers, but in our 1.5 code we still would use directives for dom manipulation.
What is the purpose of $element, $attrs inside of a component controller? These seem to be available for manipulation. Here is the link to the plunker off of the docs. I know they are not using $element, but it is the example I am reading. http://plnkr.co/edit/Ycjh1mb2IUuUAK4arUxe?p=preview
But in code like so ...
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>
`
})
What would be the use of $element if we are not manipulating the dom?
Link: The link function deals with linking scope to the DOM. Using Code for Compile. While defining a custom directive we have the option to define a link against which either we can define a function or we have the option to assign an object which will have pre & post function.
Component is used to break up the application into smaller components. But Directive is used to design re-usable components, which is more behavior-oriented. That is why components are widely used in later versions of Angular to make things easy and build a total component-based model.
The controller in AngularJS is a JavaScript function that maintains the application data and behavior using $scope object. You can attach properties and methods to the $scope object inside a controller function, which in turn will add/update the data and attach behaviours to HTML elements.
That's a great question. And I have a simple answer for it.
They take place in components just because Component is syntax sugar around of directive.
Before angular added Components, I was using some kind of component syntax for directives, it was like a convention, that in our project we have two kinds of directives, one is responsible for DOM manipulations, the second is directives with templates which should not manipulate DOM. After components were added, we did not more than changed names.
So Component
is nothing more than simple directive which was created as new entity which:
I think you can find even more answers in angular sources, but I advise you do not mix these entities, and in case you need to manipulate DOM inside of your component, just use directive inside.
Angular component life cycle hooks allow us to do DOM manipulation inside component controller using $element service
var myApp = angular.module('myApp');
myApp.controller('mySelectionCtrl', ['$scope','$element', MySelectionCtrl]);
myApp.component('mySection', {
controller: 'mySelectionCtrl',
controllerAs: 'vm',
templateUrl:'./component/view/section.html',
transclude : true
});
function MySelectionCtrl($scope, $element) {
this.$postLink = function () {
//add event listener to an element
$element.on('click', cb);
$element.on('keypress', cb);
//also we can apply jqLite dom manipulation operation on element
angular.forEach($element.find('div'), function(elem){console.log(elem)})
};
function cb(event) {
console.log('Call back fn',event.target);
}
}
declare component in html
<my-section>
<div class="div1">
div 1
<div>
div 1.1
</div>
</div>
<div class="div2">
div 1
</div>
component's partial template(./component/view/section.html)
<div>
<div class="section-class1">
div section 1
<div>
div section 1.1
</div>
</div>
<div class="section-class1">
div section 1
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With