【发布时间】:2016-10-05 22:38:06
【问题描述】:
我正在尝试使用 BrowserDomAdapter 在 Angular2 上下文之外更改文本值,它工作正常。
但更改值并未反映在模型/组件/指令上。 我实际上是在 anuglar2 中寻找 $apply 类似的功能。 我知道我们可以通过使用 Zone.js 来实现它 即使它不起作用。
以下是我的代码 sn-p,感谢任何帮助/建议。
` //应用组件 从“@angular/core”导入{组件}; 从'./default.directive'导入{DefaultDirective}; @零件({ 选择器:'我的应用', templateUrl:"./templates/default.tpl.html", 指令:[DefaultDirective] }) 导出类 AppComponent { 颜色:字符串=“红色..”; }
// default.tpl.html
<div>
===>{{color}}
<input type="text" [(ngModel)]="color" [defaultColor]="color"/>
</div>
import { Directive, ElementRef, Input, NgZone } from '@angular/core';
import {BrowserDomAdapter } from '@angular/platform-browser/src/browser/browser_adapter';
@Directive({
selector: '[defaultColor]',
})
export class DefaultDirective {
private el: HTMLElement;
dom:BrowserDomAdapter;
constructor(el: ElementRef,public zone:NgZone) {
this.el = el.nativeElement;
this.dom = new BrowserDomAdapter();
}
ngOnInit() {
this.zone.runOutsideAngular( ( )=>{
this.zone.run(()=>{
this.dom.setValue(this.el,"Adding some content fro ngOnInit");
// I am changing value of a text field out side of angular context.
// Value is updating but model is not reflecting
//Here i can use @Input/@Output but I would like to change it in out side of angular context.
});
});
}
}
`
【问题讨论】:
标签: angular angular2-directives