问题
I am trying to pass a simple string into an angular 2 component. For the life of me I can't find the issue. I am passing the value in as a hardcoded value on the component from index.html
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<accordian [inputField]="'string'">Loading...</accordian>
</body>
</html>
Component
import { Component, Input } from '@angular/core';
@Component({
selector: 'accordian',
templateUrl: 'app/accordian.html',
styleUrls: ['app/accordian.css']
})
export class Accordian {
@Input() inputField;
expanded = true;
expandToggle(){
console.log(this.inputField)
this.expanded = !this.expanded;
}
}
Template
<header (click)="expandToggle($event)">
<h4>Select an Activity!!!</h4>
<button>{{expanded ? '-' : '+'}}</button>
</header>
<ul *ngIf="expanded">
<li>
<h5>{{inputField}}</h5>
<a href="#">Update Personal Information</a>
</li>
<li>
<h5>User Tools</h5>
<a href="#">Update Personal Information</a>
</li>
<li>
<h5>User Tools</h5>
<a href="#">Update Personal Information</a>
</li>
</ul>
All I get is undefined when I click on the plus button to spit out the value. Any help would be fantastic!
回答1:
Angular doesn't do any binding from index.html
only from within other components.
A workaround to get attribute values in the root component is
export class AppComponent {
constructor(elementRef:ElementRef) {
console.log(elementRef.nativeElement.getAttribute('inputField));
}
}
See also https://github.com/angular/angular/issues/1858
回答2:
I am not sure you can do this from index.html
Usually to use Components (or Directives) you need to specify them as directives with the @Component
or @Directive
decorators.
I should start with the standard AppComponent
approach of the QuickStart and use Accordian within AppComponent.
I hope this helps.
回答3:
Here is the link to working plunker code
Index.html
<!DOCTYPE html>
<html>
<head>
<title>angular2 playground</title>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/2.0.0-beta.17/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<my-app>
loading...
</my-app>
</body>
</html>
main.ts
import {bootstrap} from '@angular/platform-browser-dynamic';
import {App} from './app';
bootstrap(App, [])
.catch(err => console.error(err));
app.ts
import {Component} from '@angular/core';
import {Accordian} from './accordian';
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<accordian [inputField]='"string"'></accordian>
</div>
`,
directives: [Accordian]
})
export class App {
constructor() {
this.name = 'Angular2 (Release Candidate!)'
}
}
accordian.ts
import {Component, Input} from '@angular/core'
@Component({
selector: 'accordian',
providers: [],
template: `
<header (click)="expandToggle($event)">
<h4>Select an Activity!!!</h4>
<button>{{expanded ? '-' : '+'}}</button>
</header>
<ul *ngIf="expanded">
<li>
<h5>{{inputField}}</h5>
<a href="#">Update Personal Information</a>
</li>
<li>
<h5>User Tools</h5>
<a href="#">Update Personal Information</a>
</li>
<li>
<h5>User Tools</h5>
<a href="#">Update Personal Information</a>
</li>
</ul>
`,
directives: []
})
export class Accordian {
@Input() inputField: string;
expanded = true;
expandToggle(){
console.log(this.inputField)
this.expanded = !this.expanded;
}
}
来源:https://stackoverflow.com/questions/37149618/issue-passing-value-into-angular-2-component