angular controller not returning result

143 Views Asked by At
<html ng-app="myApp">
<head>
<script src=angular.js></script>
<script src=app.js></script>
</head>
<body>
<div ng-controller="mainController">
{{name}}
</div>
</body>
</html>

app.js file

angular.module('myApp',[]);
var mainController = function($scope){
$scope.name="peter";    
}

I must get a return of name "peter" but unfortunately I am not getting that. Can someone help me out?

2

There are 2 best solutions below

0
On

Change your controller to be dependent on module

var app = angular.module('myApp', []);

app.controller('mainController', ['$scope', function($scope) {
    $scope.name="peter"; 
}]);

here is the workin Plnker

0
On

Modify your app.js as below:

var app = angular.module('myApp', []);

app.controller('mainController', [$scope, function($scope) {
    $scope.name="peter"; 
}]);