Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法使用“角度材质选择”上的“渲染器2”设置禁用的属性_Angular_Angular5_Angular Material2 - Fatal编程技术网

Angular 无法使用“角度材质选择”上的“渲染器2”设置禁用的属性

Angular 无法使用“角度材质选择”上的“渲染器2”设置禁用的属性,angular,angular5,angular-material2,Angular,Angular5,Angular Material2,我无法使用渲染器2禁用角度材质的“选择”下拉列表。下面是我的代码 Component.html <mat-select #exLoc (selectionChange)="someFun($event)" [(value)]="someVal"> <mat-option aria-selected="true" [value]="locVal" *ngFor="let location of locations">{{l

我无法使用渲染器2禁用角度材质的“选择”下拉列表。下面是我的代码

Component.html

            <mat-select #exLoc (selectionChange)="someFun($event)" [(value)]="someVal">
              <mat-option aria-selected="true" [value]="locVal" *ngFor="let location of locations">{{location.LocationName}}
              </mat-option>
            </mat-select>

exLoc
的类型从
ElementRef
更改为
MatSelect

constructor(public renderer: Renderer2) {} 
@ViewChild('exLoc') exLoc: MatSelect;

functionToDisableDropDown() {
  // not sure why is this not working
  //this.renderer.setAttribute(this.exLoc, 'disabled', 'true');

  // without using renderer
  this.exLoc.disabled = true;
}

工作

正确的方法实际上是使用Render2

disabled是一个属性,这就是它不能与代码一起工作的原因

正确代码:

this.renderer.setProperty(this.exLoc, 'disabled', true);
this.renderer.setProperty(this.exLoc, 'disabled', false);

使用->this.render.setProperty(this.exLoc.nativeElement,'disabled',true);
这个“.nativeElement

我知道这是一个老问题,但真正的问题可能在于MatSelect是一个自定义元素,它实际上没有禁用的属性。MatSelect可能有自己的管理模式。因此,如果您想禁用MatSelect元素,您应该有一个对该组件的引用,并使用它自己的setDisabledState方法(从ControlValueAccessor接口实现)

例如:

 @ViewChild(MatSelect)
 private _myMatSelect: MatSelect;

 ...

 xxx() {
     this._myMatSelect.setDisabledState(true); // Should trigger the engine to disable your MatSelect element.
 }

试过了,没用。我最终在html中添加了[disabled]=“value”,并在.ts文件中将其设置为true和false,而不是调用renderr2方法来获得所需的结果。但是我仍然不明白为什么上面的代码不起作用。它在没有使用像
this.exLoc.nativeElement.disabled=true这样的渲染器的情况下工作吗是的,正如我所说,我得到了期望的结果。但是我的问题是为什么它不能与Renderer2一起工作这可能是一个不接受MatSelect2作为输入的bug。你在哪里以及如何调用functionToDisableDropDown()functionToDisableDropDown(){this.isDropDownDisabled=true;}@Vijay Sankhat:是的,最终我只做了那件事。但这为什么不起作用让我很困扰。@Vega:在其他按钮的click事件上调用该函数。该方法正在被调用。我用控制台检查了一下。log@RaviYadav然后请向上投票我的评论请解释你的答案并使用反勾号格式化代码,以便更容易理解和阅读。您好,欢迎使用so!请编辑帖子,并在答案中设置代码格式。因为它将证明你的答案是正确的,并对你试图解释的内容给出清晰的看法。
setDisabledState(isDisabled: boolean): void
 @ViewChild(MatSelect)
 private _myMatSelect: MatSelect;

 ...

 xxx() {
     this._myMatSelect.setDisabledState(true); // Should trigger the engine to disable your MatSelect element.
 }