小编Abo*_*diz的帖子

使用反应式表单时将模板引用变量绑定到 ngModel

我正在使用反应式表单。当输入状态无效时,我会显示错误。这是我的观点:

<div class="form-group">
    <label for="username">Username</label>
    <input name="username"
           type="text"
           class="form-control"
           id="username"
           formControlName="username"
           #username/>
    <div class="alert alert-danger"
         *ngIf="formDir.form.controls.username.touched && 
                formDir.form.controls.username.invalid">
        This field is required.
    </div>
</div>

<div class="form-group">
    <label for="password">Password</label>
    <input type="password"
           id="password"
           class="form-control"
           name="password" />
</div>

<pre>{{myForm.value | json}}</pre>
<button class="btn btn-primary" type="submit">Sign in</button>
Run Code Online (Sandbox Code Playgroud)

每次我想使用 ngIf 显示验证错误时,我都必须编写这个笨拙的代码:

*ngIf="formDir.form.controls.username.touched && 
       formDir.form.controls.username.invalid">
Run Code Online (Sandbox Code Playgroud)

当你有更多的对象需要验证时,它会成为更大的迫害者。

通过关注 angular.io 上的文档和这个示例,我找到了一个解决方案,但我必须为我想要在视图中访问它的每个表单控件创建一个实例。

我正在寻找一种解决方案,例如我们可以在模板驱动验证中使用的解决方案,使用临时变量和 ngModel,如下所示:

<input type="text" class="form-control" name="username" #username="ngModel">
<div *ngIf="username.touched && username.invalid" class="alert alert- 
danger">Email is required</div> 
Run Code Online (Sandbox Code Playgroud)

据我从这个链接了解到,没有办法实现这一点,但这个链接是旧的,它可能在新版本的角度中存在解决方案。

你能帮助我吗?

谢谢

angular2-forms angular

5
推荐指数
1
解决办法
1809
查看次数

在 asp.net 核心控制器中,HTTP 上下文为空

我正在使用 ASP.Net Core 2.1.1。在我的控制器中调用HttpContext 时遇到问题。当我想使用HttpContext 时,程序返回NullReferenceException并说HttpContext.get返回 null。

我很困惑,因为它在控制器内。你能帮我找出可能的原因吗?

购物车控制器.cs

public class CartController : Controller
{
    private readonly IProductServices _productServices;
    private readonly ICartServices _cartServices;

    public CartController(IProductServices productServices, ICartServices cartServices)
    {
        _productServices = productServices;
        _cartServices = cartServices;
        cartServices.Cart = GetCart();
    }

    public RedirectToActionResult AddToCart(int productID, string returnUrl)
    {
        ProductViewModel product = _productServices.GetByID(productID);
        if (product != null)
        {
            _cartServices.AddItem(product, 1);
            SaveCart(_cartServices.Cart);
        }

        return RedirectToAction("Index", new { returnUrl });
    }

    public RedirectToActionResult RemoveFromCart(int productID, string returnUrl) …
Run Code Online (Sandbox Code Playgroud)

asp.net-core-mvc asp.net-core

3
推荐指数
1
解决办法
5332
查看次数