如何在`ng-content`中移除Angular选择器?

huangapple go评论58阅读模式
英文:

How remove angular selector in ng-content

问题

我有这个组件的嵌套结构

<form [formGroup]="form" novalidate autocomplete="off" (ngSubmit)="onSubmit()">
  <app-form-field label="Input" [required]="true" [control]="form.controls['input']">
    <app-input formControlName="input"></app-input>
  </app-form-field>
</form>

这是DOM的呈现:
如何在`ng-content`中移除Angular选择器?

如何移除app-input的Angular选择器,只保留input标签?

我需要这样来使用~>的CSS选择器。

感谢您的帮助

英文:

I have this imbrication of component

  &lt;form [formGroup]=&quot;form&quot; novalidate autocomplete=&quot;off&quot; (ngSubmit)=&quot;onSubmit()&quot;&gt;
    &lt;app-form-field label=&quot;Input&quot; [required]=&quot;true&quot; [control]=&quot;form.controls[&#39;input&#39;]&quot;&gt;
      &lt;app-input formControlName=&quot;input&quot;&gt;&lt;/app-input&gt;
    &lt;/app-form-field&gt;
  &lt;/form&gt;

this is a render of the DOM:
如何在`ng-content`中移除Angular选择器?

How can i remove the app-input angular selector and just keeping input tag please?

I need this for works with ~ or > css operator

Thanks for your help

答案1

得分: 1

你不能移除<app-input>选择器,但可能有其他解决方案:

  • 如果<app-input>中实际上没有你需要的任何逻辑,只需将其替换为原生的<input>。只要你导入了ReactiveFormsModuleformControlName将在原生的<input>标签上正常工作,你不总是需要一个组件。
  • 如果你确实需要app-input组件
    • 使用后代元素上的类,而不是>~,以特定地定位你想要样式化的元素,无论它们如何嵌套
    <form [formGroup]="form">
      <app-form-field>
        <app-input formControlName="input" class="form-control"></app-input>
      </app-form-field>
    
      <app-form-field>
        <input type="checkbox" formControlName="input2" class="form-control" />
      </app-form-field>
    
      <input type="radio" formControlName="input3" class="form-control" />
    </form>
    
    • 使用选择器的组合,例如
    form app-form-field > *,
    form app-form-field app-input input {
      // 你的样式
    }
    
英文:

You can't remove the &lt;app-input&gt; selector, but there may be other solutions to your problem:

  • If you don't actually have any logic in &lt;app-input&gt; that you need, just replace this with a native &lt;input&gt;. formControlName will work on native &lt;input&gt; tags out of the box (as long as you have imported the ReactiveFormsModule), you don't always need a component
  • If you do have a need for the app-input component
    • Use classes on descendent elements instead of &gt; or ~ to specifically target the elements you want to style no matter how they are nested
    &lt;form [formGroup]=&quot;form&quot;&gt;
      &lt;app-form-field&gt;
        &lt;app-input formControlName=&quot;input&quot; class=&quot;form-control&quot;&gt;&lt;/app-input&gt;
      &lt;/app-form-field&gt;
    
      &lt;app-form-field&gt;
        &lt;input type=&quot;checkbox&quot; formControlName=&quot;input2&quot; class=&quot;form-control&quot; /&gt;
      &lt;/app-form-field&gt;
    
      &lt;input type=&quot;radio&quot; formControlName=&quot;input3&quot; class=&quot;form-control&quot; /&gt;
    &lt;/form&gt;
    
    • Use a combination of selectors, e.g.
    form app-form-field &gt; *,
    form app-form-field app-input input {
      // your style
    }
    

huangapple
  • 本文由 发表于 2023年2月14日 01:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定