【问题标题】:Angular Directives in Html helperHtml 助手中的 Angular 指令
【发布时间】:2015-01-02 08:12:00
【问题描述】:

如何在 ASP.NET MVC C# 中将 Angular directives 包含在 Html Helper 中。 我不知道如何包含它。 我所做的如下:

 @Html.EditorFor(model => model.Questions.Question , new{ng_model="quest"})
                    {{quest}}

但是当我寻找没有ng-model directive的源代码时?

【问题讨论】:

    标签: c# html angularjs-directive razorengine


    【解决方案1】:

    Html.EditorFor 的第二个参数用于扩展 ViewData。 它由模板使用。默认模板不使用您的附加条目。 但它支持 htmlAttributes

    @Html.EditorFor(model => model.Questions.Question , new { htmlAttributes = new { ng_model="quest" }})
    

    或者在没有任何模板的情况下渲染它

    @Html.TextBoxFor(model => model.Questions.Question, new { ng_model = "quest" })
    

    【讨论】:

    • 您添加 htmlAttributes 选项的解决方案不起作用。如果我使用您删除模板的第二个想法,它会显示 ng-model 指令。任何想法为什么?
    • EditorFor 的第一个方法在additionalViewData 参数中给出了htmlAttributes。如果您使用的是自定义 EditorFor 模板,您需要再次将 htmlAttributes 传递给下一个调用/模板在您的自定义编辑器模板中,例如:@Html.TextBox("", Model.Value, ViewData["htmlAttributes"])
    【解决方案2】:

    要在 Html Helper 中包含 Angular 指令,您应该传递一个空参数作为第二个参数,然后在对象中传递 Angular 指令。

    @Html.DropDownList("ViewBag-Name", null, new { ng_model = "Model-Name"})
    

    其中 ViewBag-Name 包含从控制器传递给视图的 SelectList 对象。

    【讨论】:

      【解决方案3】:

      我们可以很容易地做到这一点。

      @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @ng_change = "call()" ,@class="form-control" }})

      【讨论】:

      • 请仅在与答案直接相关的情况下链接到您的网站,并明确说明它是您的网站。否则,这将被视为垃圾邮件。
      • @Glorfindel 也许您可以在 meta 中阅读此讨论。 meta.stackoverflow.com/questions/342356/…
      • @Steve Erm?那个讨论是关于回滚战的?它与 this 答案有什么关系?
      • @Steve 谢谢,我读了,但我怀疑我是否可以添加一些有意义的东西。 (而且好像和这个用户没有关系,但可能是因为看到我在这里才ping到我这里?)
      • @Steve Ah,我明白你现在的意思了。这个答案仍然有一些有价值的内容,该网站与手头的问题有关,现在用户应该了解 Stack Overflow 的自我推广规则,如果你是新手,学习这些规则可能会很棘手。
      【解决方案4】:

      @Html.TextBox("Text1", null, new { ng_model = "model1" })

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 2013-02-16
        • 2013-06-29
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        相关资源
        最近更新 更多