How to make the content appear in the DOM only after clicking - Angularjs

Asked

Viewed 447 times

1

Hello, I am beginner with Angularjs.

To insert and delete new fields this is all ok, see fiddle: http://jsfiddle.net/zbdvke2w/4/

It happens that I need that when loading the page there is not all the content that has inside the div Jumbotron in the DOM

<div class="jumbotron">
      <button style="float:right " type="button" class="btn btn-danger" ng-click="removerIcms(icms)">x</button>
      <label for="ex2">Regime</label>
      <select class="form-control" ng-model="icms.regime" ng-options="r.regime for r in data">
        <option value="">Selecione o Regime</option>
      </select>
      <label for="ex2" ng-show="icms.regime">Situação Tributária
        <select class="form-control" ng-show="icms.regime" ng-model="icms.situacao" ng-options="s.situacao for s in icms.regime.SituacaoTributaria">
          <option value="">Selecione a Situação Tributária</option>
        </select>
      </label>
      <label for="ex2" ng-show="icms.regime">Origem
        <select class="form-control" ng-model="icms.Origem" ng-options="o.Origem for o in icms.regime.Origens">
          <option value="">Selecione a Origem do Produto</option>
        </select>
      </label>
    </div>

How do I insert the content just after clicking? This is already working, but I need that when loading the screen have only the include button and is not ever loaded the content of the div Jumbotron.

3 answers

1


You could do this in a lot of ways. I believe the simplest would be the way @Johnny Gabriel quoted it: create a control variable and change it.

In your Jsfiddle code, you can see that you have added an element within the initial vector $scope.ICMSs = [{}];

So that there is no element, you can leave this vector empty.
$scope.ICMSs = [];

So there will only be the include button.

  • +1 For the good answer, my answer is a more greedy alternative, kkkk.

  • hahaha an evil that is enough for everyone. Thanks for the feedback

  • 1

    @Gabrielcâmara, perfect, thank you very much

  • Dispose! Glad you helped! D

0

A simple way to do this is by removing the first one, using its removal method within a method init() inside your controller:

 $scope.init = function(el) {
     $scope.removerIcms(el);
  }
 $scope.init(0);

Here the working example.

Another way would be to create in your object another parameter called status. And bring only those with the status true. in the ng-if="icms.status == true", and in the ng-click button action you would call a sequential action, changing the status of the list elements of your array / object.

  • Ivan, could you ask me a question? The directive ngIf removes the DOM element, correct? If the variable value is changed to true, the element is added dynamically in the DOM again?

  • 1

    @Gabrielcâmara, exactly and I found this amazing, since it can have content in html facilitating programming and only appear in DOM when requested

  • Thank you very much for the clarification, @Gih

  • It does not remove from the gift exactly, it will just not appear within the condition, which in case is your loop, ng-if is a condition that will make the value check, as per the parameter of your collection and the equality, difference, higher, lower, true, false, etc. Depending on the condition you place.

-1

Use a combination of ng-show/Hide with a control variable that changes value after clicking.

  • 2

    Friend, there is an example code missing. For it to be a good answer, it is valid that there is demonstration of how to do it.

  • Thank you for answering @Johnnygabriel, in the case Hide only hidden visually but still in DOM (console), ng-if may be solution, but in my case the only way was to follow the tip of Gabriel Câmara

Browser other questions tagged

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