Right like ionize said, in Angular2 you have 2 different bindings:
- Property-Binding:
[myProp]="myValue"
- Event-Binding:
(eventName)="myEventCallback($event)"
With property-binding you can set existing props like:
<div [style.backgroundColor]="bgColor"></div>
or you can add own properties mostly for @Inputs of another component
<my-component [shouldKnowAbout]="thisValues"></my-component>
@Component({
selector: 'my-component'
})
class MyComponent {
@Input() shouldKnowAbout; // Know your component has a custom Property/Input-Binding
...
}
You have noticed the []
brackets? --> this is an indicator that you want to pass a class member as value and a non static value. If you only want to set the value only once, you do not need the brackets!
In this way you can pass data down your component tree.
=============================================================================
But sometimes you do not only want to pass data to a child component, often you need to inform your parent component about something happened --> this is similar to listen to an event. But step by step.
The easy case --> a simple event listener like click, submit, … --> you can use all the native events
<button (click)="myCallback($event)">Click me</button>
If you want to listen, to an event --> you need the curly-brackets ;).
Now, our component from above get’s an output to share data with the parent:
<my-component [shouldKnowAbout]="thisValues" (myOutputEvent)="handleEvent($event)"></my-component>
@Component({
selector: 'my-component'
})
class MyComponent {
@Input() shouldKnowAbout; // Know your component has a custom Property/Input-Binding
@Output() myOutputEvent = new EventEmitter()<string> // event-emitter of type string
...
}
So in some cases you can trigger your myOutputEvent
with a string value:
myOutputEvent.emit('Hello');
You have noticed the $event
--> it is a predefined thing in Angular >=2 (you can see that with prefix $
).
$event
holds the event-data. for native events it is simple the whole event-object you know from JavaScript.
For our custom event $event
holds your event data --> the string “Hello”
============================================================================
To your problem:
But you are using typescript --> so you can type your classes, variabels, class members, parameters, return values of a function. So the ts compiler checks on build-time, if everything is working --> but only the ts files.
But in your case --> your are using a function in the template, which is not defined in your class --> BOOOooooom Error, because you want to call a function, that is not existing.