boomler
boomler

Reputation: 21

AngularJS: the controller function is not a function got undefined

I am a AngularJS beginer.Here is my HTML:

 <div class="col-md-12 footer" ng-controller="User">
  {{user.username||"ad"}}

 </div>

Here is my controllers.js:

angular.controller('User', user);
function User($scope){
    $scope.user={
        'username':"wang",
        'password':"wanghao"
    }
}

And I linked the controller in the (I am sure the link is right):

<script src="js/Angular/angular.js"></script>
      <script src="js/Angular/controllers.js"></script> 

But,when I run my code,here is the result:

{{user.username||"ad"}} 

Here is the error: enter image description here I am really confused.I tried to write the JS in the HTML,or link the Javascript before the controller.But it still didn't work!!

Upvotes: 0

Views: 73

Answers (3)

 <div class="col-md-12 footer" ng-app="app" ng-controller="User">                
    {{user.username||"ad"}}
 </div>

 //---Controller---//
 var app = angular.module('app', []);
 app.controller('User', function($scope){
 $scope.user={
    'username':"wang",
    'password':"wanghao"
 }
 });

Upvotes: 0

Charlie
Charlie

Reputation: 23778

The function names are wrong in the controller and the function.

Just change it to function user($scope)

OR

You can declare your controller function directly like this:

angular.controller('User', function($scope){

});

Upvotes: 0

FesterCluck
FesterCluck

Reputation: 320

It's this part, capitalization on your function.

angular.controller('User', user);
function user($scope){
    $scope.user={
        'username':"wang",
        'password':"wanghao"
    }
}

Upvotes: 1

Related Questions