Issue
I am practicing Angularjs and made a simple module and controller. Im trying to display an elements data inside the controller on the HTML view page but the {{student.name}} only pops up. and im getting an error message saying:
angular.js:15697 Error: [$controller:ctrlreg] http://errors.angularjs.org/1.8.2/$controller/ctrlreg?p0=MyController
Here is my js code:
var myApp= angular.module('myApp',[]);
myApp.controller=('MyController', function($scope){
$scope.student={
"name":"kevin",
"age":"21",
"school":"NYU"
};
});
Here is my HTML code:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<!-- angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="app.js"></script>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body ng-controller="MyController">
{{student.name}}
</body>
</html>
please help.
Solution
The way you have defined your controller is incorrect. You can do something like below:
angular.module('app').controller('MainCtrl', function ($scope){
$scope.student={
"name":"kevin",
"age":"21",
"school":"NYU"
};
});
Sample working code here: https://stackblitz.com/edit/angularjs-controllers-eauszw?file=app.js
Answered By - Sagar Agrawal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.