如果用户输入不是this示例后的字母数字,我试图显示错误,但没有成功。始终显示错误消息。
这是代码段:
<div class="col-md-4" *ngIf="this.context.risk.contractOrigin === 'Appel_d_offre'">
<label class="control-label" for="numberTenderInvitation">Numéro d'appel d'offre</label>
<input class="form-control" id="numberTenderInvitation" name="numberTenderInvitation"
[(ngModel)]="context.risk.policyId"
#numberTender="ngModel"
maxlength="10"
[pattern]="'^[a-zA-Z \-\']$'"
required/>
</div>
<small class="errorLabel" [hidden]="!numberTender.errors">
Le champ comporte une erreur.
</small>
我得到的错误是:
错误类型错误:无法读取未定义
的属性“错误”
我哪里出错了?
答案 0 :(得分:5)
你的模式有一些问题
1。它只检查单个字符
^字符串的开始
[...]匹配1
$ end of string
如果您需要至少一个字符,则需要添加“+”,如果为0或更多,则为“*”
2。你说非字母数字,但你没有专门检查数字,你应该为你的模式添加0-9
3。尝试在[\ _]之后移动[\ - ],这样你的最终模式如下(假设你想要至少一个字符作为输入)
[pattern]="'^[a-zA-Z0-9 \'\-]+$'"
答案 1 :(得分:2)
为了便于参考,我发布了最终解决方案:
<div class="col-md-4" *ngIf="this.context.risk.contractOrigin === 'Appel_d_offre'">
<label class="control-label" for="numberTenderInvitation">Numéro d'appel d'offre</label>
<input class="form-control" id="numberTenderInvitation" name="numberTenderInvitation"
#numberTender="ngModel"
[(ngModel)]="context.risk.policyId"
maxlength="10"
[pattern]="'^[a-zA-Z0-9 \'\-]+$'"
required/>
<small class="errorLabel" [hidden]="numberTender.pristine || numberTender.valid">
Le champ comporte une erreur.
</small>
</div>
答案 2 :(得分:1)
您错过了elvis operator。这是必需的,因为您的HttpContent z = new StringContent("{\"status\": \"approved\",\"comments\": \"" + Request.QueryString["comment"].ToString() + "\"}", Encoding.UTF8, "application/json");
public static async Task PatchAsync(Uri requestUri, HttpContent content)
{
try
{
using (HttpClient client = new HttpClient())
{
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, requestUri)
{
Content = content
};
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", "XXXXXXXXX"))));
//using (HttpResponseMessage response = await client.PostAsync(requestUri, content))
using (HttpResponseMessage response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
respApproval = responseBody;
}
}
}
catch (Exception ex)
{
respApproval = ex.ToString();
}
}
元素可能会尝试访问numberTender,因为它已完全初始化。试试这个:
<small
我还删除了<small class="errorLabel" [hidden]="numberTender?.errors">
Le champ comporte une erreur.
</small>
,因此该逻辑显示为:如果数字投标存在且它有错误属性
答案 3 :(得分:0)
尝试一下。
<small [hidden]="numberTender.valid" class="errorLabel" >
Le champ comporte une erreur.
</small>