I'm currently working on a basic angular component. I have set up a parameter as a binding and managed to display its value on the screen successfully. The parameter displays correctly, but when I try to access it within the controller, it shows undefined in the console log.
Check out the code below:
var app = angular.module("test", []);
app.component("test", {
bindings: {
"contactId": "<"
},
controllerAs: "model",
controller: () => {
//output: 'contact id from controller: undefined'
console.log(`contact id from controller: ${this.contactId}`);
},
template: "<div>Contact id from view: {{model.contactId}}</div>"
});
Here is how you would include it in HTML:
<test contact-id="8"></test>
It's puzzling that the binding works in the view but not in the controller. Can anyone pinpoint what might be causing this issue?
You can view the problem in action on Plunker.