0
I have the following code listing categories:
<div class="row" ng-show="!TemCategoria">
<div class="container" id="categoria" style="float:right" ng-hide="TemCategoria">
<select class="btn btn-warning pull-right" onchange="window.location.href=this.value" style=" margin-right: 20px" ng-show="!TemCategoria">
<option selected ng-show="!TemCategoria">@Model[0].CategoriaAtual.ToString()</option>
@for (int i = 0; i < Model[0].CategoriaId.Count; i++)
{
<option ng-show="!TemCategoria" style="color:#000; background-color:white" value="@Url.Action("Index", "Produto", new {CategoriaId = Model[0].CategoriaId[i], area = "Lojista" })">@Model[0].NomeCategoria[i]</option>
}
</select>
</div>
I’m assigning the value to variable TemCategoria
thus:
@{
var TemCategoria = @Model[0].TemCategoria;
}
I need that when there is no category the code simply does not appear and is not interpreted, because I get an error because category is empty.
I tested it when it has category and it hides in case I put ng-show="TemCategoria"
and shows if I put ng-show="!TemCategoria"
but when there is no category it simply gives the null variable error, it simply ignores the ng-show
, someone knows what I must do to fix this?
Ps:. I put the TemCategoria
in all to test.
Select your angular code?
– novic
ng-show and ng-Hide just work with the
display
of the element, or it hides or it shows. ie, it will be interpreted because it exists in the code. You will have to do this check differently, I advise you to do it directly in c# because the angular is "Client Side". it will be interpreted only after the compilation of your code.– Aldo Fernandes Junior
I thought Angular really removes the code =/ , I’ll see how to solve it on the side of c# , complicated and that is to appear dropdown only if it has category, I don’t know if I can do it. but see the answer there .
– William Cézar
@Williamcézar he can remove, just use
ng-if
, see my answer.– celsomtrindade
@Williamcézar your
TemCategoria
is an angular variable or C#? I think if you do this server side validation, it is easier and better depending on your business rule.– Aldo Fernandes Junior