3
我正在使用自定义指令,它应该为主机设置值属性。 问题是它不更新组件的模型,只更新元素值。Angular 2自定义指令不更新模型
这里是直播plnkr链接:https://plnkr.co/edit/lcT4q9EP3OEnuIDcGobC?p=preview
//our root app component
import {Component} from 'angular2/core';
import { Directive, ElementRef, OnInit, HostListener } from 'angular2/core';
@Directive({selector: '[myData]'})
class MyDataDirective implements OnInit {
private el: any;
constructor(el: ElementRef) {
this.el = el.nativeElement
}
@HostListener('focus') onFocus() {
console.log("focus event triggered...")
this.el.setAttribute("value", "On Focus value") //Input value changes but model doesn't update
}
ngOnInit() {
console.log("oninit function called...")
this.el.setAttribute('value', 1234)
}
}
@Component({
selector: 'my-app',
template: `
<input type="text" placeholder="Enter text" [(value)]="inputValue" myData/>
`;
directives: [MyDataDirective]
})
export class App {
constructor() {
this.inputValue = "Value from model"
}
}