【发布时间】:2016-12-03 21:52:30
【问题描述】:
我创建了一个自定义控件指令作为“my-text-box”,在这个指令中我使用 html 控件<input>,我需要将值从我的自定义控件传递到 html 输入控件。在 Angular1 中,我只是在使用 <my-text-box id="1" name="Box 1"> 之类的指令,在指令中使用范围变量的自定义属性,我将值分配给 html 控件,例如 <input type="text" id="{{id}}" name={{name}} > 我如何在 Angular 2 中使用这种场景。
这是我的示例代码:
AppComponent.ts
import {Component} from '@angular/core';
import {TextBoxComponent} from './TextBoxComponentDirective';
@Component({
selector: 'my-app',
template: '<h1>{{title}}</h1> <my-text-box id="66" name="MyText1"></my-text-box>',
directives:[TextBoxComponent]
})
export class AppComponent {
title="Sample TextBox";
}
TextBoxComponentDirective.ts
import {Component,Input,Directive} from '@angular/core';
@Component({
selector:'my-text-box',
templateUrl: './TextBoxTemplate.html',
})
export class TextBoxComponent {
@Input() id;
}
TextBoxTemplate.html
<input type="text" [name]="name" [id]="id"/>
【问题讨论】:
-
在 angular2 中,指令没有 Angular1 中的模板元属性。所以需要代码并向我们展示您到目前为止所做的工作。
-
@micronyks 更新
标签: html typescript angular angular2-directives