Javascript for the Angular

Asked

Viewed 286 times

3

I’m kind of new at Angular, could someone help me convert this code into Javascript for angular.

	window.onload = function(){
		var campo1 = document.getElementById("campo1").value;

		if(campo1 == 0.00){
			document.getElementById("icon").style.display = "none";
		} else {
			document.getElementById("icon").style.display = "block";

		}
	}
<input type="checkbox" name="check" id="checkbox" />
<input type="text" id="campo1" value="0.00" />
<div id="icon">
	<img src="lupa.png" />
</div>

  • Controller Research. And on the Angular homepage, you can already solve this question.

2 answers

3

  • Link the value of campo1 a variable in the current scope;
  • use this value as a comparison for via ng-if or ng-show.

Functional example to follow:

function SampleController($scope, $filter) {
  $scope.valor = 0.00;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app>
  <body>
    <div ng-controller="SampleController">
      <input type="text" ng-model="valor" />
      <div id="icon" ng-if="valor!=0" >
        <img src="http://orig05.deviantart.net/c360/f/2012/049/c/3/lupa_png_by_hannaabigail1-d4q6jmc.png" />
      </div>
    </div>
  </body>
</html>

  • Thank you very much, it helped a lot. .

  • @Diboa Always a pleasure to help! I did not understand right, could elaborate uses question a little more?

  • So friend, in my input already comes a fixed value value for example value="10".

  • @Diboa value will always be overwritten by the value of the tied model.

  • vlw Worked Right, also managed using ng-show="".

0

Normally, you set the starting values for your form in the controller. This way you will have an initial value defined when the application starts.

Look at this example I wrote: https://jsbin.com/halerewuba/1/edit?html,output

<!DOCTYPE html>
<html ng-app="exemploApp">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <div ng-controller="ExemploCtrl as ctrl">
      <form>
        <p>
          <label>Nome:</label>
          <input type="text" ng-model="ctrl.nome">
        </p>

        <p>
          <label>Idade:</label>
          <input type="number" ng-model="ctrl.idade">
        </p>

        <button type="submit">Submit</button>
      </form>

      <div>
        <h3>Debug:</h3>
        <p>
          {{ctrl}}
        </p>
      </div>
    </div>

    <!-- AngularJS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
    <script>
      angular.module('exemploApp', [])
        .controller('ExemploCtrl', function() {
          var ctrl = this;

          // Valores iniciais para os campos do formulário
          ctrl.nome = "";
          ctrl.idade = 18;

        })
      ;

    </script>
  </body>
</html>

Browser other questions tagged

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