亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法訪問html模板中的this.text

無法訪問html模板中的this.text

偶然的你 2023-09-04 17:06:34
我正在嘗試添加兩個按鈕,它們只應在輸入字段中有一些文本時顯示,否則默認情況下應隱藏/禁用它,但我收到此錯誤。<div class="card card-body">`<form>`    `<div class="form-group">`        `<input type="text" class="form-control" placeholder="Add a Log ...">`        `<input type="submit" value="Add Log" class="btn btn-light" [disabled]=!this.text>`        `<button class="btn btn-warning" [hidden]=!this.text>Clear </button>`    `</div>`</form></div>錯誤:清除~~~~~~~~~         src/app/components/log-form/log-form.component.ts:5:16         templateUrl: './log-form.component.html',                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~       Error occurs in the template of component LogFormComponent.
查看完整描述

3 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

您可以使用 ngModel 輕松實現此目的,如下所示:


mycomp.component.html


<form>

 <div class="form-group">

     <input type="text" class="form-control" name="text" [(ngModel)]="textvalue" placeholder="Add a Log ...">

     <input type="submit" value="Add Log" class="btn btn-light" [disabled]="!textvalue">

     <button class="btn btn-warning" [hidden]="!textvalue">Clear </button>

 </div>

 {{textvalue}}//contains value of your input element.

</form>

mycomp.component.ts


export class MyComponent {

  textvalue:string;

}

請務必在 app.module.ts 文件的 import[] 數組中導入 FormsModule,以便 ngModel 正常工作。此外,輸入的 name 屬性也很重要,或者您可以使用 formControlName。


希望這可以幫助?。。?/p>


查看完整回答
反對 回復 2023-09-04
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

反引號內不需要反引號

"text"不是this.text


`<form>

    <div class="form-group">

    <input type="text" class="form-control" placeholder="Add a Log ...">

    <input type="submit" value="Add Log" class="btn btn-light" [disabled]="!text">

    <button class="btn btn-warning" [hidden]="!text">Clear </button>

    </div>

</form>`


查看完整回答
反對 回復 2023-09-04
?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

您缺少模板中的引號:

[disabled]="!this.text"


查看完整回答
反對 回復 2023-09-04
  • 3 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號