最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Binding to a component property in angular2 - Stack Overflow

programmeradmin2浏览0评论

I'd like to reference a property on a component within A. that' component's constructor B. that component's template. The apis on this seem to be shifting a little bit, but i'd expect the following to work:

<my-component [greeting]="hello"></my-component>
// my component.es6.js
@Component({
  selector: 'my-component',
  properties: {
   'greeting': 'greeting'
  }
})
@View({
  template: '{{greeting}} world!'
})
class App {
  constructor() {
    console.log(this.properties) // just a guess
  }
}

Plunkr

What am I doing wrong?

I'd like to reference a property on a component within A. that' component's constructor B. that component's template. The apis on this seem to be shifting a little bit, but i'd expect the following to work:

<my-component [greeting]="hello"></my-component>
// my component.es6.js
@Component({
  selector: 'my-component',
  properties: {
   'greeting': 'greeting'
  }
})
@View({
  template: '{{greeting}} world!'
})
class App {
  constructor() {
    console.log(this.properties) // just a guess
  }
}

Plunkr

What am I doing wrong?

Share Improve this question edited Jan 1, 2020 at 11:55 C.OG 6,5293 gold badges23 silver badges41 bronze badges asked May 1, 2015 at 16:37 Nick TomlinNick Tomlin 29.2k12 gold badges63 silver badges94 bronze badges 2
  • I'm not sure it's possible with the current build of Angular. I poked around to see how angular2_material handled it, but when I try those techniques, it either doesn't do anything, or they're using features not found in my angular2 (pulled from npm moments ago). (1) [md-button][href] is a simple example I've found that simply expects that tabIndex (using hostProperties, not properties) will be bound to the object, but in my code, it never is. (2) md-radio-button makes use of @Attribute, which TypeScript won't compile for me (angular2/angular2 has no exported memember 'Attribute'). – Langdon Commented May 1, 2015 at 20:30
  • @Langdon interesting, thanks for investigating! It seems like things are a little in flux now, which is to be expected. – Nick Tomlin Commented May 2, 2015 at 17:08
Add a comment  | 

3 Answers 3

Reset to default 5

I was experimenting with Angular2 and came up against the same problem. However, I found the following to work with the current alpha version (2.0.0-alpha.21)

@Component({
  selector: 'hello',
  properties: {'name':'name'}
})
@View({
  template:`<h1>Hello {{_name}}</h1>`
})
class Hello {
  _name: string;

  constructor() { 
    console.log(this);
  };

  set name(name){
    this._name = name;
  }
}

@Component({
  selector: 'app',
})
@View({
  template:
  `
    <div>
      <hello name="Matt"></hello>
    </div>
  `,
  directives: [Hello]
})
class Application {
  constructor() { };
}

bootstrap(Application);

It seems that properties on the Class that is passed to bootstrap are ignored. Unsure if this is intended or a bug.

Edit: I've just built Angular2 from source and tried the @Attribute annotation, it works as per the docs (but only on the nested component).

constructor(@Attribute('name') name:string) { 
    console.log(name);
};

Prints 'Matt' to the console.

The current way is to decorate the property as @Input.

@Component({
    `enter code here`selector: 'bank-account',
    template: `
    Bank Name: {{bankName}}
    Account Id: {{id}}
    `
})
class BankAccount {
    @Input() bankName: string;
    @Input('account-id') id: string;
    // this property is not bound, and won't be automatically updated by Angular
    normalizedBankName: string;
}
@Component({
    selector: 'app',
    template: `
    <bank-account bank-name="RBC" account-id="4747"></bank-account>`,
    directives: [BankAccount]
})
class App {}
bootstrap(App);

above example is from https://angular.io/docs/ts/latest/api/core/Input-var.html

Actually, you can do it better way. When you are defining properties in your component, you always specify it the following way:

howYouReadInClass:howYouDefineInHtml

So, you may as well do the following:

@Component({
  selector: 'my-component',
  properties: {
   'greetingJS:greetingHTML'
  }
})
@View({
  template: '{{greeting}} world!'
})
class App {
set greetingJS(value){
this.greeting = value;
}
  constructor() {

  }
}

This way you will not get conflicts in TS, and you will have a clearer code - you will be able to define the variable as you define it in partent component.

发布评论

评论列表(0)

  1. 暂无评论