Click here to Skip to main content
15,889,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I am coming across one common error in rendering simple angular code
HTML
<pre><!DOCTYPE html>
<html ng-app>

  <head>
    <script data-require="angular.js@*" data-semver="4.0.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>
    <script data-require="angular.js@*" data-semver="4.0.0" src="script.ts"></script>
    <script data-require="angular.js@*" data-semver="4.0.0" src="system.config.js">  </script>
    <script data-require="angular.js@*" data-semver="4.0.0" src="tsconfig.json"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller='mainctrl'>
    <h1>Hello Plunker!</h1>
    <p>{{ message }}</p>
  </body>

</html>






My Javascript code is:

JavaScript
let mainctrl = function($scope) {
  $scope.message='Hello Angular!';
}



When I render this code I get a line of errors

[$controller:ctrlreg] http://errors.angularjs.org/1.6.10/$controller/ctrlreg?p0=mainctrl


I am a beginner to Angular, What could be the possible problem?

What I have tried:

I tried rendering the code, but could not find any thing which could create error. I also tried removing line

JavaScript
<pre><script data-require="angular.js@*" data-semver="4.0.0" src="system.config.js">  </script>


since it showed error in this line
Posted
Updated 7-Jun-18 2:54am

1 solution

So, for Angular 1 apps you need to explicitly define the application and controllers, and they are different form one another. This may read differently from examples that you've seen for NG2+.

JavaScript
var myApp = angular.module('myApp');

myApp.controller('mainctrl',['$scope', function($scope){
   $scope.message='Hello Angular!';
}]);


The controller definition includes the dependency injection array used by NG1, and you'll notice that the name to use for the controller in the HTML is a string, not a variable.

Then amend the html declaration tag:
HTML
<html ng-app="myApp">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900