Javascript has been disabled in your browser.

3

I'm new to Angular, could anyone help me convert this code in javascript to 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>
    
asked by anonymous 19.11.2015 / 19:15

2 answers

3
  • Bind the value of campo1 to a variable in the current scope;
  • Use this value as a comparison for path ng-if or ng-show .

Functional example below:

function SampleController($scope, $filter) {
  $scope.valor = 0.00;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><htmlng-app><body><divng-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>
    
20.11.2015 / 01:31
0

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

See this example I wrote: link

<!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(){varctrl=this;//Valoresiniciaisparaoscamposdoformulárioctrl.nome="";
          ctrl.idade = 18;

        })
      ;

    </script>
  </body>
</html>
    
10.07.2016 / 06:24