【问题标题】:Angular FormControl Placeholder will not update dynamically with passed valueAngular FormControl 占位符不会使用传递的值动态更新
【发布时间】:2021-03-17 05:49:07
【问题描述】:

我希望有人可以帮助我,我在这里有点障碍。 我对 Angular 很陌生,所以请原谅我的错误。

我正在尝试更新输入 FormControl 的占位符文本,如下所示:

我有以下组件类:

export class EditFormComponent {

namePlaceholder:String="";

  restaurantForm = new FormGroup({
    id: new FormControl(''),
    name: new FormControl(''),
    address: new FormControl(''),
    specialities: new FormControl(''),
    details: new FormControl('')
  });

 passObjectValues(restaurant: RestaurantsPost) {
    this.restaurantForm.setValue({ id: restaurant.id, name: restaurant.name, address: restaurant.addressId });
    this.namePlaceholder=restaurant.name + "test";
    console.log(this.restaurantForm);
    console.log(this.namePlaceholder);
  }

HTML 组件如下所示:

<form class="container-card" [formGroup]="restaurantForm" (ngSubmit)="submit(restaurantForm)">
    <div class="form-group">
        <label style="color: white; font-weight:500;"for="name">
            Name
        </label>
        <input 
            formControlName="name"
            id="name" 
            type="text" 
            class="form-control"
            [placeholder]="namePlaceholder" // doesn't work
            placeholder="{{namePlaceholder}}"> // doesn't work 
    </div>

我假设发生这种情况是因为我没有创建我的 EditFormComponent 的实例,而是从类中调用一个方法,因此在构建我的表单组时,我传递的值没有在编译时注册。

我也尝试调用构造函数,而不是调用我的 passObjectValues() 方法,但这本身就是一个挑战。

我的构造函数是这样的:

constructor(editedRestaurant:RestaurantsPost) {

    this.templateRestaurant=editedRestaurant;

我的 RestaurantPost 对象:

@Injectable()
export class RestaurantsPost {


    id: String;
    name: String;
    addressId: String;
    specialitiesId: String;
    detailsId: String;
    bookingId: String;

    constructor(id:String,name:String,addressId:String,specialitiesId: String,detailsId: String,bookingId: String){
        this.id=id;
        this.name=name;
        this.addressId=addressId;
        this.specialitiesId=specialitiesId;
        this.detailsId=detailsId;
        this.bookingId=bookingId
    }
}

app.module.ts 看起来像这样:

@NgModule({
  declarations: [
    AppComponent,
    RestaurantComponent,
    EditFormComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    NoopAnimationsModule,
    DemoMaterialModule,
    FormsModule,
    ReactiveFormsModule
  ],
  providers: [
    HttpErrorHandler,
    HttpClientModule,
    String
  ],
  bootstrap: [
    AppComponent
  ]
})
export class AppModule { }

浏览器抛出此错误:

>    main.ts:12 Error: Can't resolve all parameters for String: (?).
>     at getUndecoratedInjectableFactory (core.js:11428)
>     at injectableDefOrInjectorDefFactory (core.js:11418)
>     at providerToFactory (core.js:11461)
>     at providerToRecord (core.js:11448)
>     at R3Injector.processProvider (core.js:11346)
>     at core.js:11332
>     at core.js:4129
>     at Array.forEach (<anonymous>)
>     at deepForEach (core.js:4129)
>     at R3Injector.processInjectorType (core.js:11332)

有什么想法吗?

谢谢!

更新:

我设法通过向打开我的编辑表单的字段添加绑定来解决我的问题:

<edit-form [selectedRestaurant] = "selectedRestaurant" *ngIf="editPressed"></edit-form>

当我单击“编辑”按钮时,我正在调用一个将当前对象的值保存到 selectedRestaurant 的方法。

<button mat-icon-button (click)="editRestaurant(restaurant,!editPressed)">

在我的 restaurant.component 中,我有这个方法可以保存我按下编辑按钮的餐厅,布尔字段用于切换编辑表单:

editRestaurant(passedRestaurant: Restaurants, isActivated:boolean): void {
        this.selectedRestaurant=passedRestaurant;
        this.editPressed=isActivated;

}

这已绑定到我的编辑表单组件中的一个对象:

@Input('selectedRestaurant') selectedRestaurant: Restaurants = {
    id: 0,
    name: '',
    address: this.templateAddress,
    specialities: this.templateSpecialities,
    details: this.templateDetails,
    booking: this.templateBookings
  };

在我的 HTML 文件中我添加了这个:

<input  
    formControlName="name" 
    id="name" 
    type="text" 
    class="form-control"
    placeholder={{selectedRestaurant.name}}> 

【问题讨论】:

    标签: angular


    【解决方案1】:

    你试过了吗

    <input type="text" #input />
    @ViewChild('input') input: ElementRef;
    this.input.nativeElement.placeholder = 'placeholder text';
    

    【讨论】:

    • 构造函数在浏览器中抛出错误。构造函数(editedRestaurant:RestaurantPost){ this.templateRestaurant = new RestaurantsPost(editedRestaurant.id,editedRestaurant.name,editedRestaurant.addressId,editedRestaurant.specialitiesId,editedRestaurant.detailsId,editRestaurant.bookingId); this.input.nativeElement.placeholder = '占位符文本'; // 添加了这个 console.log(this.restaurantForm);错误:main.ts:12 错误:无法解析字符串的所有参数:(?)。
    【解决方案2】:

    如果您删除表单控件中的占位符会怎样?删除占位符并检查此问题是否真的针对占位符。

    我希望这个问题与占位符无关。它与不必要/错过的提供者有关。所以请删除提供者中的String 并检查

    providers: [
        HttpErrorHandler,
        HttpClientModule, //this is also not needed here
        String // remove this and try 
      ],
    

    更新:

    还提供构造函数参数作为可空值或删除构造函数

    constructor(id?:String,name?:String,addressId?:String,specialitiesId?: String,detailsId?: String,bookingId?: String){
            this.id=id;
            this.name=name;
            this.addressId=addressId;
            this.specialitiesId=specialitiesId;
            this.detailsId=detailsId;
            this.bookingId=bookingId
        }
    

    【讨论】:

    • 不必要的提供者,即 String / HttpClient - 没有变化。还从表单控件中删除了占位符 - 没有变化。
    • @BogdanMaceasa 这个Can't resolve all parameters for String: (?). 错误与占位符无关。您是否删除了这些提供商并进行了检查?
    • @BogdanMaceasa 也尝试 with 更新的答案
    • 我正在控制传递给构造函数的值,并且还使用静态值调用构造函数,所以现在我认为我不需要在构造函数中进行可空检查。当我通过占位符问题时,我将在实际将其连接到我的 POST 服务之前添加可空值和其他缺失部分。
    • 还尝试删除构造函数 - 仍然是相同的错误:“main.ts:12 错误:无法解析字符串的所有参数:(?). at getUndecoratedInjectableFactory (core.js:11428) at injectableDefOrInjectorDefFactory (core.js:11418) 在 providerToFactory (core.js:11461) 在 providerToRecord (core.js:11448) 在 R3Injector.processProvider (core.js:11346) 在 core.js:11332 在 core.js:4129 在Array.forEach () at deepForEach (core.js:4129) at R3Injector.processInjectorType (core.js:11332)"
    【解决方案3】:

    所以我设法通过向打开我的编辑表单的字段添加绑定来解决我的问题:

    <edit-form [selectedRestaurant] = "selectedRestaurant" *ngIf="editPressed"></edit-form>
    

    当我点击“编辑”按钮时,我正在调用一个将当前对象的值保存到 selectedRestaurant 的方法。

    <button mat-icon-button (click)="editRestaurant(restaurant,!editPressed)">
    

    在我的 restaurant.component 中,我有这个方法可以保存我按下编辑按钮的餐厅,布尔字段用于切换编辑表单:

    editRestaurant(passedRestaurant: Restaurants, isActivated:boolean): void {
            this.selectedRestaurant=passedRestaurant;
            this.editPressed=isActivated;
    
    }
    

    这已绑定到我的编辑表单组件中的一个对象:

    @Input('selectedRestaurant') selectedRestaurant: Restaurants = {
        id: 0,
        name: '',
        address: this.templateAddress,
        specialities: this.templateSpecialities,
        details: this.templateDetails,
        booking: this.templateBookings
      };
    

    在我的 HTML 文件中我添加了这个:

        <input  
            formControlName="name" 
            id="name" 
            type="text" 
            class="form-control"
            placeholder={{selectedRestaurant.name}}> 
    

    【讨论】:

      【解决方案4】:

      我认为问题出在这里:

      namePlaceholder:String="";
      

      应该是

      namePlaceholder: string="";
      

      【讨论】:

      • 无论哪种方式呈现。但是,我将来对“namePlaceholder”所做的任何更改都不会在我的 HTML 的占位符字段中更新。我尝试过原始字符串和字符串对象,它们的结果相同。
      猜你喜欢
      • 2017-11-02
      • 2019-09-05
      • 1970-01-01
      • 2017-12-17
      • 2018-07-04
      • 2019-01-13
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多