简体   繁体   English

从 onDeactivate 访问 p-inplace 内容值

[英]access p-inplace content value from onDeactivate

I'm working with p-inplace of primeNg.我正在使用 primeNg 的 p-inplace。 I want to call a function when onDeactivate is called, using the value of pTemplate content.我想在调用onDeactivate时调用 function,使用 pTemplate 内容的值。

code snippet:代码片段:

<p-inplace [closable]="true" (onDeactivate)="doSomething(???)">
   <ng-template pTemplate="display">
      <label>xxx</label>
   </ng-template>
   <ng-template pTemplate="content">
      <input type="text" #myInput pInputText [ngModel]="someReadonlyInitialData">
   </ng-template>
</p-inpace>

I want to send the value typed into myInput where the???我想将输入到 myInput 的值发送到哪里??? are.是。 If it were not in the template, I could send by doSomething(myInput.value) , but since it is within a template, it is not recognized there.如果它不在模板中,我可以通过doSomething(myInput.value)发送,但由于它在模板中,因此在那里无法识别。

Does anyone have a solution or a workaround for this?有没有人对此有解决方案或解决方法?

Unfortunately you can not use template-variable-reference as you already know.不幸的是,您不能使用您已经知道的模板变量引用。

I solution to your problem would be to save the changes in an other variable in your template so you can use it when you need it and leave the parameter in your template function doSomething() empty.我解决您问题的方法是将更改保存在模板中的另一个变量中,以便您可以在需要时使用它,并将模板中的参数 function doSomething()留空。

<p-inplace [closable]="true" (onDeactivate)="doSomething()">
   <ng-template pTemplate="display">
      <label>xxx</label>
   </ng-template>
   <ng-template pTemplate="content">
      <input type="text" #myInput pInputText [ngModel]="someReadonlyInitialData" (ngModelChange)="someData = $event">
   </ng-template>
</p-inpace> 

And inside your ts file:在你的 ts 文件中:

someData: string;

doSomething() {
  /* now your value is inside this.someData /*
}

PS: I guess that you don't want to use 2 way binding, If you don't mind, you do not need to create a second variable. PS:我猜你不想使用双向绑定,如果你不介意,你不需要创建第二个变量。 You can just:你可以:

[(ngModel)]="someData" 

I addition to StPaulis's answer, don't forget to add name attribute, if ngModel is used within a form tag.除了 StPaulis 的回答,如果在表单标签中使用了ngModel ,请不要忘记添加 name 属性。 So the input will look like this:所以输入看起来像这样:

 <input type="text" pInputText [(ngModel)]="someData" name="someData">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM