0

I'm just starting out with Angular.js controller and I'm getting an error Uncaught TypeError: app.controlller is not a function when I try to initialize the angular controller.

Have a look at my snippet.

var app = app;
if (!app) {
  app = angular.module('app', []);
  console.log("created app");
};
app.controlller('languages', ['$scope',
  function($scope) {
    $scope.selected = 'None';
    $scope.js = function() {
      $scope.selected = 'JavaScript';
    };
    $scope.cpp = function() {
      $scope.cpp = 'C++';
    };
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app='app'>
  <div ng-controller='languages'>
    Select a language:
    <button ng-click='js()'>JavaScript</button>
    <button ng-click='cpp()'>C++</button>
    <p>You have selected {{ selected }}</p>
  </div>
</body>

3
  • There is one extra l in app.controlller. Should be app.controller. Commented May 16, 2016 at 10:19
  • Oh thanks for pointing that out! But the cpp() function still doesn't work. Any reason? Commented May 16, 2016 at 10:23
  • oh got it. there's a typo there again Commented May 16, 2016 at 10:24

2 Answers 2

1

There is TYPO error in controller

app.controlller this should be app.controller

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for pointing that out! But the cpp() function still doesn't work. Any reason?
you have created same name for function and model $scope.cpp = function() { $scope.cpp = 'C++'; };
1

Fixed typo controlller and changed cpp() function to set $scope.selected = 'C++'.

var app = app;
if (!app) {
  app = angular.module('app', []);
  console.log("created app");
};
app.controller('languages', ['$scope',
  function($scope) {
    $scope.selected = 'None';
    $scope.js = function() {
      $scope.selected = 'JavaScript';
    };
    $scope.cpp = function() {
      $scope.selected= 'C++';
    };
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app='app'>
  <div ng-controller='languages'>
    Select a language:
    <button ng-click='js()'>JavaScript</button>
    <button ng-click='cpp()'>C++</button>
    <p>You have selected {{ selected }}</p>
  </div>
</body>

1 Comment

Just write a small snippet about what you have changed here to let other readers know.

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.