Angular ng-show , does not remove C#/Angular/Javascript code

Asked

Viewed 210 times

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?

  • 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.

  • 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 .

  • @Williamcézar he can remove, just use ng-if, see my answer.

  • @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.

1 answer

2


To begin with, you must understand the concept of ng-show and ng-Hide. They will only control the visual display of the element, ie, is the equivalent to use display: none;, however, the html element will still be written on the page.

The best way to avoid this would be by using ng-if. The ng-if will not even render the HTML element if the check is not true.

Another point to note, is the way you are using the combinations, they are very complex, using even several times within the same block of code, which in some cases does not even make sense. See the table below to understand how they would work:

  • ng-show="!TemCategoria" = Vai flaunt when nay have TemCategoria definite
  • ng-show="TemCategoria" = Vai flaunt when have TemCategoria
  • ng-hide="!TemCategoria" = Vai hide when nay have TemCategoria definite
  • ng-hide="TemCategoria" = Vai hide when have TemCategoria

And with ng-if:

  • ng-if="TemCategoria" = Vai render when have TemCategoria
  • ng-if="!TemCategoria" = Vai render when there is no TemCategoria

Therefore, you should define the ng-if only in the initial block, removing all other statements from ng-show and ng-hide inside that code pad.

<div class="row" ng-if="TemCategoria">
    ... restante do código ...
</div>

Getting the final code like this:

<div class="row" ng-if="TemCategoria">
    <div class="container" id="categoria" style="float:right">
        <select class="btn btn-warning pull-right" onchange="window.location.href=this.value" style=" margin-right: 20px">
            <option selected>@Model[0].CategoriaAtual.ToString()</option>
            @for (int i = 0; i < Model[0].CategoriaId.Count; i++)
            {
                <option 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>
</div>

Thus, case nay we have TemCategoria defined, the entire block will not even be rendered in the view.

  • Fine explanation, friend, I’ll test it in my code and come back to tell the results.

  • In the case my 'Temcategoria' is a Boolean, I put ng-if but it keeps coming in.. sera I have to work with a different variable?

  • @Williamcézar Mas Temcategoria is a variable defined in your Angularjs code? For example, $Scope.Temcategoria = true; ? Because I do not know if there would be how to communicate this verification with a variable that is not of the scope of Angularjs.

  • So , funny and that when it has category it works perfectly , hides and appears, I have not set in my Angular code, this infection is coming straight from the C controller#.

  • @Williamcézar but how so, hides and appears? The purpose of ng-if, is to show or not that whole block of code when there is declaration of TemCategoria, that is, if TemCategoria = true; then the whole block will appear, select will be displayed. Now if TemCategoria = false; then nothing will appear on the screen, select and the entire block will not be displayed (whereas you are using ng-if="TemCategoria". That’s what’s going on?

  • I expressed myself badly , if I change 'Temcategoria' for '!Temcategoria' it changes between hiding and showing the code, but only when it has category , in case I’m hiding a boot.

  • @Williamcézar This is probably because using ! Temcategoria is the default value because $Scope.Temcategoria has never been set. I think you will have to do a check inside Angularjs itself and create a variable that controls this view. For example, within your controller, have an if(Temcategoria) { $Scope.Temcategoriang = true;} check and use in your view like ng-if="Temcategoriang". You’ve come to understand the logic?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.