0

I'm trying to learn angularjs and struggling with getting my second component, 'list, to render. Is something wrong with the syntax? Here's the HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Hello World, AngularJS - ViralPatel.net</title>
    <script type="text/javascript"
        src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> 
</head>
<body ng-app="myModule">

    Write some text in textbox:
    <input type="text" ng-model="sometext" />

    <h1>Hello {{ sometext }}</h1>
  <app></app>
  <script src="App.js"></script>
  <script src="List.js"></script>
</body>
</html>

Here's App.js

angular.module('myModule', [])

.component('app', {

  template:
    '<div>' +
      '<h1>Hello from App.js</h1>' +
      '<list></list>' +
    '</div>'

})

And here's List.js, the one that won't render:

angular.module('myModule', [])

.component('list', {

  template:
    '<h1>Hello from List.js</h1>'
    '<ul>' +
      '<li>1</li>' +
      '<li>2</li>' +
    '</ul>'
})

1 Answer 1

1

The problem here seems to be that you're re-declaring the myModule module twice. The module is being re-declared twice in each of your .js files, when you do the following:

angular.module('myModule', [])

A subtle "gotcha" in angular is that a new module is declared internally in AngularJS when you pass [] as an second argument to angular.module (see documentation)

Try revising your List.js to this and see if that helps:

/* Remove the [] to prevent myModule from being re-defined. 
   We assume myModule has been defined during the execution of App.js, 
   which should happen before List.js is executed */
angular
.module('myModule') 
.component('list', {

  template:
    '<h1>Hello from List.js</h1>'
    '<ul>' +
      '<li>1</li>' +
      '<li>2</li>' +
    '</ul>'
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that helped (and I noticed a missing + in the template, afterwards). So now renders on the screen but I do get an error in the console that says "Uncaught Error: [$injector:nomod] errors.angularjs.org/1.5.6/$injector/nomod?p0=myModule" . Edit: nevermind, fixed that bug after following the link in the error. Thanks!

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.