Iv'e recently started a new project and decided to give Angular 2 a chance. As a former Backbone developer, I am now facing types of work which are really new to me.
I am trying to build a very simple app: I want to have a class room object, and within it a list of students. In my point of view, each of them (classroom, student) should be different components, such that each component has it's own template.
I wish to have the templates completely separated, and on the classroom template to loop through the students and render the student template.
For instance: My classroom component would like like that: import {Component, OnInit} from '@angular/core'; import {Student} from 'app/student.component';
@Component({
selector : 'classroom',
templateUrl : 'app/classroom.component.html',
directives : [Classroom]
})
export class MainContainer implements OnInit {
students : Student[]; // assume I have an array of students here
constructor() {}
}
The Classroom template would look:
<div>
<student *ngFor="let student of students"></student>
</div>
The Student component:
import {Component, Input, Output} from '@angular/core';
@Component({
selector : 'student',
templateUrl : 'build/component/main-box/student.component.html'
})
export class Student {
name : string;
id: number;
grade:number;
constructor() {
}
};
And the student template, as simple as:
<div>
name : {{name}}
id : {{id}}
grade : {{grade}}
</div>
But the above code shows me nothing. Seems like the student data is not passed to the student object. Iv'e seen some examples that passed the entire object down, this way:
[student]="student"
But it feels really wrong. In Backbone for instance, I would render the parent view, and inside it append all the child views. Here it feels odd.
Another solution Iv'e seen is to simply add the student template to the classroom template, instead of holding them in separate files. I must say that I really really don't like this practice since I believe that these components should'nt live together in the same file.
Another things, what doest @Input() stand for? I did not really understand from the docs why @Input() gives access to data.
At this point I am more confused than not :] I would really appreciate some tips and your feedback and learn some good practices for dealling such tasks.
Thanks a lot!