Eu pensei que soubesse ou pelo menos tivesse entendido Angular, mas quando tentei criar uma função de acordo com um execício, surgiu uma dúvida, como eu associo funções passando parâmetros?
Como funciona o this?
Para tudo precisa do $scope?
Esse é o código que eu estou tentando criar uma função que dependendo da quantidade de caracteres, aparece uma mensagem no HTML:
<!doctype html><html lang="en">
<head><title>Lunch Checker</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="styles/bootstrap.min.css"><script src="js/angular.min.js"></script>
<script src="js/app.js"></script><style>.message { font-size: 1.3em; font-weight: bold; }
</style></head><body ng-app="LunchChecker"><div class="container" ng-controller="LunchCheckerController"><h1>Lunch Checker</h1><div class="form-group">
<input id="lunch-menu" type="text" placeholder="list comma separated dishes you usually have for lunch" class="form-control" ng-model="name" ng-keyup="displayNumeric();"></div>
<div class="form-group"> <button class="btn btn-default" ng-click="displayNumeric();">Check If Too Much</button></div><div class="form-group message"><!-- Your message can go here. -->
{{sayMessage()}} </div></div></body></html>
(function () {
use strict';
angular.module('LunchChecker', [])
.controller('LunchCheckerController', LunchCheckerController);
LunchCheckerController.$inject = ['$scope'];
function LunchCheckerController($scope) {
$scope.name = "";
$scope.totalValue = 0;
$scope.displayNumeric = function () {
var totalNameValue = calculateString($scope.name);
$scope.totalValue = totalNameValue;
console.log(totalNameValue)
$scope.sayMessage = function(totalNameValue) {
console.log(totalNameValue)
if(totalNameValue >= 3) {
return "Enjoy!";
} else {
return "Too much!";
}
};
};
function calculateString(string) {
var totalStringValue = 0;
var stringLength = string.length;
totalStringValue = stringLength;
return totalStringValue;
}
};
})();