0

I am new to angularJS and trying to get the following simple samples to work. But when I ran it, I got a blank screen instead of "Hello world". Help will be greatly appreciated. Thanks.

angular-comp.js:

  angular.module('myApp').component('greetUser', {
      template: 'Hello, {{$ctrl.user}}!',
      controller: function GreetUserController() {
          this.user = 'world';
      }
 });

index.html:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="ISO-8859-1">
    <title>AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js">             </script>
    <script src="js/angularcomp.js"></script>
</head>
<body>
    <greet-user></greet-user>
</body>
 </html>

Update: I found the problem, the version 1.4.5 doesn't support component. I now use 1.6.1 and it works !!!!

2
  • I'm working on this.. wait a minute Commented Feb 9, 2017 at 21:59
  • 1
    you probably meant angular.module('myApp', []) instead of angular.module('myApp'). Commented Feb 9, 2017 at 22:09

1 Answer 1

1

The problem resides here

angular.module('myApp')

where this should be

angular.module('myApp',[])

because you're creating a new module. If you don't pass the second Array argument, AngularJS will try to find your module instead of creating a new one.

try this:

<!DOCTYPE html>
<html>

  <head>
    <meta charset="ISO-8859-1">
    <title>AngularJS</title>
  </head>

  <body ng-app="myApp">
    <greet-user></greet-user>
  </body>

  <script>
    angular.module('myApp',[])
      .component('greetUser', {
        template: 'Hello, {{$ctrl.user}}!',
        controller: function GreetUserController() {
          this.user = 'world';
        }
      });

  </script>

</html>
Sign up to request clarification or add additional context in comments.

3 Comments

- thiagoh I tried the solution, but still didn't see the "Hello world" on the web page.
I copied the above solution in my IDE, and ran it again but still didn't see the "Hello world" on the web page.
Did you include the last version of angular 1?

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.