我已经对此有所了解,但我似乎无法找到关于不同浏览器如何处理事物的任何信息.
对于我的一些组件,我想改变输入字段readonly和来回的必需属性.
我已经设法获得一个正在运行的代码,它可以根据需要更改它们,但问题是它只适用于readonly,但似乎没有按要求工作:虽然元素属性更改为所需的Angular2仍然认为fieldCtrl是有效的.
这是我的plunker,我在这里说明了这个问题:https://plnkr.co/edit/Yq2RDzUJjLPgReIgSBAO?p = preview
//our root app component
import {Component} from 'angular2/core'
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<form #f="ngForm">
<button type="button" (click)="toggleReadOnly()">Change readonly!</button>
<button type="button" (click)="toggleRequired()">Change required!</button>
<input id="field" [(ngModel)]="field" ngControl="fieldCtrl" #fieldCtrl="ngForm"/>
{{fieldCtrl.valid}}
</form>
</div>
`,
directives: []
})
export class App {
constructor() {
this.name = 'Angular2'
}
toggleRequired(){
this.isRequired = !this.isRequired;
var fieldElement = <HTMLInputElement>document.getElementById('field');
if (this.isRequired){
fieldElement.required = true;
this.field = "it's required now";
}
else{
fieldElement.required = false;
this.field = …Run Code Online (Sandbox Code Playgroud) 当我尝试在我的 Angular 6 应用程序中的 formGroup 上使用 disable 方法时,我在浏览器控制台中收到此错误:
类型错误:this.personForm.disable 不是函数
尽管文档中提到了该方法,并且VS Code 甚至在此快照中建议了该方法。
我的代码在这里:
// ... many parts skipped, the form here is template driven
// but still personForm is a FormGroup ,
// I am trying to disable the whole FormGroup with all child elements
@ViewChild('personForm') personForm: FormGroup;
if (true) {
// if I console.log here the form, I can see it is created already
console.log(this.personForm);
// output of console.log is
// NgForm {submitted: false, …Run Code Online (Sandbox Code Playgroud)