将传递值发送到 Angular 2 组件
Issue passing value into Angular 2 component
我正在尝试将一个简单的字符串传递到 angular 2 组件中。对于我的生活,我找不到问题。我将值作为组件上的硬编码值从 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>
组件
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;
}
}
模板
<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>
当我点击加号按钮吐出值时,我得到的都是未定义的。
任何帮助都会很棒!
我不确定您是否可以从 index.html
执行此操作
通常要使用 Components(或 Directives),您需要使用 @Component
或 [= 将它们指定为指令12=] 装饰器。
我应该从 QuickStart 的标准 AppComponent
方法开始,并在 AppComponent 中使用 Accordian。
希望对您有所帮助。
这是link工作
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;
}
}
Angular 仅在其他组件内不对 index.html
进行任何绑定。
在根组件中获取属性值的解决方法是
export class AppComponent {
constructor(elementRef:ElementRef) {
console.log(elementRef.nativeElement.getAttribute('inputField));
}
}
我正在尝试将一个简单的字符串传递到 angular 2 组件中。对于我的生活,我找不到问题。我将值作为组件上的硬编码值从 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>
组件
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;
}
}
模板
<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>
当我点击加号按钮吐出值时,我得到的都是未定义的。 任何帮助都会很棒!
我不确定您是否可以从 index.html
通常要使用 Components(或 Directives),您需要使用 @Component
或 [= 将它们指定为指令12=] 装饰器。
我应该从 QuickStart 的标准 AppComponent
方法开始,并在 AppComponent 中使用 Accordian。
希望对您有所帮助。
这是link工作 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;
}
}
Angular 仅在其他组件内不对 index.html
进行任何绑定。
在根组件中获取属性值的解决方法是
export class AppComponent {
constructor(elementRef:ElementRef) {
console.log(elementRef.nativeElement.getAttribute('inputField));
}
}