Amio.io
Amio.io

Reputation: 21585

Example of $postLink() in components

I can't find any example of $postLink() from AngularJS components, part Components have a well-defined lifecycle. Can you please provide a simple example just of the method implented in a controller where I could realize how to manipulate DOM?

The only article explaining $postLink() a little is http://blog.thoughtram.io/angularjs/2016/03/29/exploring-angular-1.5-lifecycle-hooks.html .

It seems like that there is no argument in $postLink(). Thus I would inject $element and modify the DOM.

Upvotes: 7

Views: 8361

Answers (1)

Amio.io
Amio.io

Reputation: 21585

So the usage is simple:

function Controller($element) {
        var self = this;

        self.$postLink = $postLink;

        function $postLink() {
            $element.attr("mymymy", "xxxxxx");
        }
}

Then let's say my component name is myComponent and using an inspector of a browser you could see a modified tag with an added attribute from above: <my-component mymymy="xxxxxx"></my-component>.

Upvotes: 12

Related Questions