@Model.punkt1 @Model.punkt1 返回一个 JValue,然后我将其转换为字符串以检查长度。这是针对 Umb-6ren">
gpt4 book ai didi

javascript - 检查 JValue 字符串在 AngularJS 中是否有值

转载 作者:行者123 更新时间:2023-11-28 04:14:23 25 4
gpt4 key购买 nike

我正在我的 li 元素上执行 ng-if,以检查是否显示它。

<li ng-if="@Model.punkt1.ToString().Length > 0">@Model.punkt1</li>

@Model.punkt1 返回一个 JValue,然后我将其转换为字符串以检查长度。这是针对 Umbraco 中的网格编辑器,使用了 razor 语法。我的范围仅包含字符串:

$scope.control.punkt1;

但是当我运行页面时出现此错误:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at ASP._Page_app_plugins_mygrideditor_infobox_cshtml.Execute() in c:\Users\thomb\Documents\Visual Studio 2017\Projects\LoevbjergRema1000Umbraco\LoevbjergRema1000Umbraco\LoevbjergRema1000Umbraco\App_Plugins\MyGridEditor\infobox.cshtml:line 8
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model)
at ASP._Page_Views_Partials_grid_editors_base_cshtml.Execute() in c:\Users\thomb\Documents\Visual Studio 2017\Projects\LoevbjergRema1000Umbraco\LoevbjergRema1000Umbraco\LoevbjergRema1000Umbraco\Views\Partials\Grid\Editors\Base.cshtml:line 19

为什么我无法检查字符串的长度?

编辑:

<li ng-show="@Model.punkt1 != null &&  @Model.punkt1.ToString().Length > 0">@Model.punkt1</li>
<li ng-show="@Model.punkt2 != null && @Model.punkt2.ToString().Length > 0">@Model.punkt2</li>
<li ng-show="@Model.punkt3 != null && @Model.punkt3.ToString().Length > 0">@Model.punkt3</li>
<li ng-show="@Model.punkt4 != null && @Model.punkt4.ToString().Length > 0">@Model.punkt4</li>
<li ng-show="@Model.punkt5 != null && @Model.punkt5.ToString().Length > 0">@Model.punkt5</li>

还有我的 Angular Controller :

angular.module("umbraco").controller("my.custom.grideditorcontroller", function ($scope) {
$scope.control.heading;

$scope.control.punkt1 = "";
$scope.control.punkt2 = "";
$scope.control.punkt3 = "";
$scope.control.punkt4 = "";
$scope.control.punkt5 = "";
});

最佳答案

在将值转换为 string; 之前检查 null 条件

试试这个

<li ng-if="@Model.punkt1 != NULL &&  @Model.punkt1.ToString().Length > 0">@Model.punkt1</li>

了解更多详情:Cannot perform runtime binding on a null reference - Empty Excel Cells

请选择 ng-show而不是ng-if 。你可以看到差异:what is the difference between ng-if and ng-show/ng-hide

编辑:

您不能使用Convert.Tostring();因为你正在尝试获取字符串的长度。因此,如果值为 null ,则会引发相同的错误。您应该引用此链接 Difference between Convert.ToString() and .ToString() 。所以在这里你应该检查 null获得 length 之前的条件我上面提到的值(value)。

关于javascript - 检查 JValue 字符串在 AngularJS 中是否有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45994664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com