Angular 2.0 and ng-model
Angular 2.0 and ng-model
我正在为一个关于Angular 2迁移的演示构建一个演示应用程序。我的部分应用程序有<input ng-model="" />
,我想把它改成"Angular 2的方式"。所以,我有两个选项:
- 使用'formDirectives',这对我的演示来说是多余的,因为我这里没有表单,只有一些更新数据的输入
- 使用Victor Savkin(来自Angular 2团队)在他的(伟大的)帖子中展示的内容:
<input ([ng-model])="todo.text" />
当ng-model
为指令时:
import {Directive, EventEmitter} from 'angular2/angular2';
@Directive({
selector: '[ng-model]',
properties: ['ngModel'],
events: ['ngModelChanged: ngModel'],
host: {
"[value]": 'ngModel',
"(input)": "ngModelChanged.next($event.target.value)"
}
})
export class NgModelDirective {
ngModel: any; // stored value
ngModelChanged: EventEmitter; // an event emitter
}
我已经在我的演示项目中这样实现了:
import {Component, View} from 'angular2/angular2';
import {NgModelDirective as NgModel} from '../ng-model/ng-model';
@Component({
selector: 'font-size-component',
properties: [
'font'
]
})
@View({
template: `<input id="fontSize" class="form-control" name="fontSize" ([ng-model])="font.fontSize"/>`,
directives: [
NgModel
]
})
export class FontSizeComponent {
constructor() {
}
}
我的输入正在用提供的数据呈现(属性绑定[ng-model正在工作],但事件绑定不工作,给出以下错误:
EXCEPTION: TypeError: Cannot read property 'observer' of undefined
EXCEPTION: TypeError: Cannot read property 'location' of undefined
EXCEPTION: TypeError: Cannot read property 'hostView' of undefined
当我从ng-model
指令中删除这行events: ['ngModelChanged: ngModel'],
时,所有错误都消失了…
我对Angular 2很陌生(我们可能都是),并试图理解我在这里做错了什么…
编辑
好的,所以在阅读了更多的内容之后,我确信使用formDirectives
并不是这样的小题大做。我的解决方案是(使用Angular 2 Alpha 35现在是FORM_DIRECTIVES
而不是formDirectives
):
import {Component, View, FORM_DIRECTIVES} from 'angular2/angular2';
@Component({
selector: 'font-size-component',
properties: [
'fontSize'
]
})
@View({
template: `<input id="fontSize" class="form-control" name="fontSize" [(ng-model)]="fontSize"/>`,
directives: [
FORM_DIRECTIVES
]
})
export class FontSizeComponent {
constructor() {
}
}
你必须为你的指令初始化events
事件发射器。你可以在控制器中设置:
import { EventEmitter, Directive } from 'angular2/angular2';
@Directive({
selector: '[ng-model]',
properties: ['ngModel'],
events: ['ngModelChanged: ngModel'],
host: {
"[value]": 'ngModel',
"(input)": "ngModelChanged.next($event.target.value)"
}
})
export class NgModelDirective {
ngModel: any; // stored value
ngModelChanged: EventEmitter; // an event emitter
constructor() {
this.newModelChanged = new EventEmitter(); // <== INITIALIZATION
}
}
或者,如果你使用TypeScript,在你的属性定义中:
// ...
export class NgModelDirective {
ngModel: any; // stored value
ngModelChanged = new EventEmitter(); // <== INITIALIZATION
}
- 从 Javascript 中设置 ng-model name
- Toggle Switch在包含ng model和ng repeat之后不进行切换
- 在Angularjs中,我如何使用ng repeat、ng model和ng click来动态更改内容
- 当与之关联的 ng-model 为 false 时,Angularjs 复选框值不会取消选中
- ng-model 绑定到选择标签生成的空默认值
- 对象不能在angularjs ng-model中访问
- 为什么 HTML 中的 Value 属性在与 AngularJS 中的 ng-model 一起使用时不起作用
- ng-repeat不绑定ng-model对象数组
- AngularJS:ng-model 绑定在使用 jQuery 更改时不会更新
- 选择/选项 ng-model 未绑定 ng-selected
- Angular ng-model 将数据作为数组发送到 rails-api
- 为什么 AngularJS 在使用 ng-model 时不会在文本框上设置长度属性
- AngularJS:Ng:init-如何分配Ng:model
- 在指令中使用 ng-model 和 ng-repeat
- 使用 angularjs 访问控制器中的 ng-model 数据
- ui-bootstrap timepicker and ng-model value converter
- Difference between ng-model="xxx" and value="
- Angular 2.0 and ng-model
- Angular select and ng-model
- angularJS - ng-if and ng-model