1

Don't be rude i'm new with angularJS

I write a get function who request an API that give me some Chuck Norris Fact .

My question is simple : How to display the response in my HTML when i click on my button .

Actually when i click on my button i get my chuckNorrisFact sentence in my console .

Thanks a lot

here is my html

<!DOCTYPE html>
<html lang="FR">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Random Chuck Norris jokes</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">
</head>



<body ng-app="mainApp">

    <div ng-controller="mainCtrl">
        <button ng-click="generateRandomJoke()"> have fun</button>
        </div>
        <p></p>
    </div>

</body>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="controllers/controllers.js"></script>

</html>

And here is my controllers.js :

var app = angular.module('mainApp', []);

app.controller('mainCtrl', function ($scope, $http) {
    function getJoke() {
        $scope.generateRandomJoke = function () {
            $http({
                method: "GET",
                url: "https://api.chucknorris.io/jokes/random",
            })
                .then(function mySuccess(response) {
                    $scope = response.data;
                    console.log($scope.value)
                }, function myError(response) {
                    console.log("erreur : ", response.data.errors);
                });
        }}
    getJoke();
})




1
  • you need to add scope of data you want to show in the controller, and put this expression {{ your_scope_name }} in your HTML. For more information, you can read more here. For example (controller): $scope.result = response.data; (HTML): {{result.value}} Commented Jan 23, 2021 at 16:27

1 Answer 1

1

What you're doing is not working because you're overwriting your entire $scope variable with a string. What you have to do is to add the string as a variable inside your $scope:

var app = angular.module('mainApp', []);

app.controller('mainCtrl', function ($scope, $http) {
    function getJoke() {
        $scope.generateRandomJoke = function () {
            $http({
                method: "GET",
                url: "https://api.chucknorris.io/jokes/random",
            })
                .then(function mySuccess(response) {
                    $scope.joke = response.data.value;
                    console.log($scope.joke)
                }, function myError(response) {
                    console.log("erreur : ", response.data.errors);
                });
        }}
    getJoke();
})

Now you can use {{ }} to access the variables that are inside the scope of your current controller.

<!DOCTYPE html>
<html lang="FR">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Random Chuck Norris jokes</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">
</head>



<body ng-app="mainApp">

    <div ng-controller="mainCtrl">
        <button ng-click="generateRandomJoke()"> have fun</button>
        <p>{{ mainCtrl.joke }}</p> <!-- you can either write mainCtrl.joke or simply joke -->
    </div>

</body>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="controllers/controllers.js"></script>

</html>

See in Plunker.

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

3 Comments

Unfortunatly it doesn't work : / if you got any idea i take it
Sorry about that. I haven't tested when posting. I edited my answer and I believe it will be okay now. Tested using plunker
Perfect ! i understand now . thanks a lot

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.