0

I have a simple component and what I'm trying to do is to add to my input element an attribute multiple dynamically but somewhy it doesn't work. Why? And is there any way to do what I want?

app.component('myComponent', {
    templateUrl: 'tmpl.html',
    bindings: {
        str: '@'
    },
    controller: function () {
        var ctrl = this;
        ctrl.$postLink = function () {
            $('#myInputId').attr('multiple', '');
        }
    }
}
1
  • @lenilsondc yeah, thanks Commented Jan 9, 2019 at 10:46

1 Answer 1

0

This is the solution:

app.component('myComponent', {
    templateUrl: 'tmpl.html',
    bindings: {
        str: '@'
    },
    controller: function ($element) {
        var ctrl = this;
        ctrl.$postLink = function () {
            $element.find('input').attr('multiple', 'multiple');
        }
    }
}

The trick was in linking stuff. Code from the question doesn't work because the input not exists in DOM yet at the time $postLink hook triggered. So what we need is to inject $element service into controller and use it to manipulate DOM.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.