简体   繁体   English

Angular 2 alpha 22,如何通过属性将值传递给组件?

[英]Angular 2 alpha 22, How to pass down a value to a component through attributes?

I declared a component with a selector 'app' then I used it like follow: 我用选择器'app'声明了一个组件,然后像下面这样使用它:

<app title="Awesome Title"></app>

in the template I wrote this: 在模板中,我这样写:

The title passed {{ title }}

I did add write the Component annotation: 我确实添加了编写组件注释:

@Component({
    selector: 'app',
    properties: {title:'title'}  // also tried ['title']
});

But nothing shows up, the output is 但是什么都没有显示,输出是

The title passed

Any help? 有什么帮助吗? Thanks 谢谢

At the time of writing the most recent version is alpha.26. 在撰写本文时,最新版本是alpha.26。 The property definition has changed slightly, so here is the new syntax 属性定义略有变化,因此这是新语法

@Component({
    selector: 'app',
    properties: ['title:title']
});

<div [title]="Some Title"></div>

I have started a series of blog articles to demo Angular 2 concepts. 我已经开始撰写一系列博客文章来演示Angular 2概念。 Check it out here: 在这里查看:

http://www.syntaxsuccess.com/viewarticle/angular-2.0-examples http://www.syntaxsuccess.com/viewarticle/angular-2.0-examples

I have a working example of how to assign properties to components via properties. 我有一个工作示例,说明如何通过属性将属性分配给组件。 Check out the tree view example as it uses it heavily. 查看树视图示例,因为它使用率很高。

For some reason, properties don't seem to work on the bootstrapped root app. 由于某些原因, properties似乎在引导的根应用程序上不起作用。

Try creating a component called 'title-app' and loading it inside of 'app'. 尝试创建一个名为“ title-app”的组件,并将其加载到“ app”内部。

// inner component
@Component({
  selector: 'title-component',
  properties: {title:'title'}
})
@View({ template: '<h1>{{title}}</h1>'})
class titleComponent{}

// root app
@Component({ selector: 'app' })
@View({ 
  directives: titleComponent, 
  template: '<title-component [title]="Some Title"></title-component>' 
})
class App{}

You might also want to use the latest version of Angular2: currently alpha-26 . 您可能还想使用Angular2的最新版本:当前为alpha-26 From alpha-26+, if your property is the same name you can just call it once. 从alpha-26 +起,如果您的媒体资源是相同的名称,则只需调用一次即可。

properties: {'title': 'title'} => properties: 'title' properties: {'title': 'title'} => properties: 'title'

the correct way to do this is: 正确的方法是:

import {
  ComponentAnnotation as Component,
  ViewAnnotation as View, bootstrap
} from 'angular2/angular2';
import {Attribute} from 'angular2/src/core/annotations_impl/di';


@Component({
  selector: 'app'
})
@View({
  template: '{{goofy}} stuff'
})
class App {
  goofy:string;
  constructor(@Attribute('goofy') goofy:string) {
      this.goofy = goofy;
  }
}

bootstrap(App);

See the full Plunker here http://plnkr.co/edit/n2XWez69HNJbixH3tEmR?p=preview 在此处查看完整的Plunker http://plnkr.co/edit/n2XWez69HNJbixH3tEmR?p=preview

However, this is currently broken and is a known issue https://github.com/angular/angular/issues/1858 但是,这目前已被破解,是一个已知问题https://github.com/angular/angular/issues/1858

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM