15

Hi. I'm new to Angular. I'm testing Angular 2.0.

I read the tuto here and the guide here. In the tuto, the template is specified in the @Component annotation whereas in the guide it is in the @View annotation. So I was wondering what are the differences between the two approaches ? I looked up in the api preview but the explanations were not clear.

2
  • 2
    There's no real difference. @View is just optional Commented Oct 28, 2015 at 21:10
  • Please have a look here Commented Oct 24, 2016 at 9:33

4 Answers 4

25

Update

@View() was removed (I think in beta.13, the CHANGELOG.md doesn't mention it though).

Original

There are no differences between them. It's just sugar that you can specify all view configuration into Component so there's no need to import View decorator.

But at the same time there's a need to remain View decorator exist, because it allows us to use different views for the same component depending on language or media type. For example:

@Component(/* ... */) @View({ media: 'desktop', template: 'Template for desktop' }) @View({ media: 'mobile', template: 'Template for mobile' }) extends class Component() {} 

This feature is not implemented yet.

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

5 Comments

This may not hold true in future as per Igor in his comments in github.com/angular/angular/pull/4566 "the view switching logic will be defined elsewhere. likely via DI. we'll see."
does one component has more than one view as you described in the answer ?
@alexpods also this is not working for me 'media type' or i m not understand properly what you said. will you please provide me the demo(plnkr or fiddle) of your working code using two @view in the same component.
@Pardeepjain It's not implemented yet. I've just wanted to explain why we have separate View decorator and how it can be used in the future. I was relying on this and this github issues. Sorry for confusion.
okay i thought this is working feature of the angular2. btw thnx for clearing difference between component and view decorators.
4

As said by @Alexpods in answer and @Eric in comment before when angular2 is in alpha @view is juts optional because all the properties in the @view annotation is also included in the @component annotation so @view is just sugar that you can specify all view configuration into Component so there's no need to import View decorator.

Updated to beta

@View has been deprecated in latest release, So you can't use it.

if you are using still @view annotation it may cause producing some kind of error. so Only Component is the place that will hold the all options.

As per officials , @View metadata decorator has been deprecated in beta.10 release.

Comments

2

As per the ChangeLogs of Angular v2.0.0-beta.11, it is mentioned under breaking changes that @View() annotation (previously deprecated) has been removed. Apps should use the @Component() decorator instead.

Please have a look in Change logs of Angular2 here.

Comments

1

First of all, this was deprecated and now fully gone!

2.0.0-beta.10 (2016-03-17): @View() annotation (previously deprecated) has been removed. Apps should use the @Component() decorator instead.

So you don't need to worry about it anymore, The @View was previously introduced because the early thinking was that there could be multiple views in a component (like a mobile view for example) and the usage was as below:

 import { Component } from '@angular/core'; @Component({ selector: 'app-root', styleUrls: ['./app.component.scss']}) @View({ media: 'desktop', template: '<h1>tablet<h1>' }) @View({ media: 'mobile', template: '<h1>mobile<h1>' }) extends class Component() {} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.