[英]How to check in angularjs if ng-model has a value of string or a number?
[英]Check if JValue string has a value in AngularJS
我正在對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.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條件string;
嘗試這個
<li ng-if="@Model.punkt1 != NULL && @Model.punkt1.ToString().Length > 0">@Model.punkt1</li>
有關更多詳細信息: 無法對空引用執行運行時綁定-空的Excel單元格
並請使用ng-show
而不是ng-if
。 您可以看到區別: ng-if和ng-show / ng-hide有什么區別
您不能使用Convert.Tostring();
因為您正在嘗試獲取字符串的長度。 因此,如果這些值為null,則將引發相同的錯誤。 您應該參考此鏈接以了解Convert.ToString()和.ToString()之間的區別 。 因此,在獲取我上面提到的值的length
之前,這里應該檢查一個null
條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.