Problem with module creation and localization by Angularjs

Asked

Viewed 48 times

0

Look at the code;

<!DOCTYPE html ng-app="helloWword">
<html>
<head>
    <title>Ola Mundo</title>
    <script src="angular.js"></script>
    <script>
        angular.module("helloWword", []);
        angular.module("helloWword").controller("helloWwordCtrl", function ($scope) {
            $scope.message = "Ola mundo";
        });
    </script>
</head>
<body>
<div ng-controller="helloWwordCtrl">
    {{message}}
</div>
</body>
</html>

As you can see I’m creating the modem here on this line;

angular.module("helloWword", []);

And here I am getting the module located;

    angular.module("helloWword").controller("helloWwordCtrl", function ($scope) {
        $scope.message = "ESTA APARECENDO NO SITE";
    });
</script>

and by the line of code was to appear IS APPEARING ON THE SITE in the browser screen, and that’s not what happens, it just appears {{message}}

I wonder what I did wrong?

  • 1.x or 2.x angular?

  • Have you tried the ng-app="helloWword" in the <body>? works for me... http://jsfiddle.net/s8wp9pob/

1 answer

1


Your mistake is in the first two lines:

<!DOCTYPE html ng-app="helloWword">
<html>

You need to declare the app at the beginning of html and not in the file type declaration. Although a best practice is to declare the ngApp in the body as Sergio has already commented. To work the code should be something like:

<!DOCTYPE html>
<html ng-app="helloWorld">
<head>
  <title>Hello World</title>
</head>
<body>
  <div ng-controller="teste">
    {{message}}
  </div>

  <script src="angular.min.js"></script>
  <script>
  var app = angular.module("helloWorld", []);

  app.controller('teste', function ($scope) {
    $scope.message = "Hello from the other side!!";
  });

  </script>
</body>
</html>
  • I would like to take a doubt, is there any way to have Thymeleaf and Angularjs in the same project? i think there is no way why it is necessary to put the tags s of Thymeleaf to work and in the same place that put the tags s of Thymeleaf is the same place where put the tag of Angularjs, if put the tag s of Thymeleaf Angularjs does not work.

Browser other questions tagged

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