0

I am a very beginner at HTML5, CSS and Angular. I tried to write a simple code to program a button that could change the value of a boolean, just to check how the connections are done. The code is the following:

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>

<body>
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click="myFunction">Click Me!</button>
<br>
<p>
{{myBool}}
</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.myBool = "true";
    $scope.myFunction = function() {
      if ($scope.myBool == true) {
        $scope.myBool = false;
      } else {
      $scope.myBool = true;
      }
};
});
</script>
</body>
</html>

But when I press the button, nothing happens. Can someone tell me why? Any help is welcome because, as you can see, I am quite lost.

1 Answer 1

1

You are missing () at the end of myFunction. It should be: <button ng-click="myFunction()">Click Me!</button>

Also, you have initialised $scope.myBool as a String rather than a bool: $scope.myBool = "true" should be $scope.myBool = true

Sign up to request clarification or add additional context in comments.

2 Comments

Great, that worked! And how do I initialize it as a bool, @keithm ?
@user2211939 - use $scope.myBool = true;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.