Reputation: 2503
I am currently doing some research about Angular 2 and it seems like a lot of stuff changes so i wanted to rebuild a very common use case for my projects which looks something like this in Angular 1 to get the basics. But i stumbled upon some general questions about how to structure an Angular 2 application.
Let me give you an example how i solve this in Angular 1 right now.
I do not need a routing
<html ng-app=my-app>
<head></head>
<body>
<my-autocomplete></my-autocomplete>
<!-- Some server side code -->
<my-navigation-menu></my-navigation-menu>
<!-- Some server side code -->
<my-main-app>
<my-filter facet="one" />
<my-filter facet="two" />
<my-filter facet="three" />
</my-main-app>
</body>
</html>
Now for the Angular 2 structure.
<head>
<base href="/elasticsearch_frontend/">
<title>Angular 2 QuickStart</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- 1. Load libraries -->
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
myapp: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('myapp/main')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-autocomplete></my-autocomplete>
<!-- Some server side code -->
<my-main-app></my-app>
So the problem is the following:
It seems like that everything must be in the root of the application. I am more used to the Angular 1 style, where i used to use the directives to html tree. Now in Angular 2 i used the application component as a container to initialize my application.
main.ts
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {AppComponent} from './app.component';
bootstrap(AppComponent, [ROUTER_PROVIDERS]);
app.ts
import {Component, OnInit} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {SearchService} from './search.service';
import {CatalogComponent} from './catalog.component';
import {AutocompleteComponent} from './autocomplete.component';
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
`,
directives: [AutocompleteComponent],
providers: [
SearchService
]
})
export class AppComponent implements OnInit {
public title = 'Elasticsearch Products Prototype';
public queryParams : String[] = [];
ngOnInit() {
}
}
Upvotes: 1
Views: 1478
Reputation: 268
Micronyks is right : Components annotations (@Component) need a template or templateUrl attribute whereas Directices annotations dont - so consider using directives if you don't need html to replace.
About the structure, if you take a look at TourOfHeroes Google tutorial (https://angular.io/docs/ts/latest/tutorial/) you will find out that you need :
And for your folder structure you can have a folder for your components, services, templates,..
Upvotes: 1