The controller function received an undefined value

When setting up an AngularJS project using TypeScript, I encountered the following error:

Error: [ng:areq] http://errors.angularjs.org/1.5.5/ng/areq?p0=MovieCtrl&p1=not%20aNaNunction%2C%20got%20undefined

I am also utilizing watchify to compile my app.ts into bundle.js

In my index file, Angular is bootstrapped using ng-app, including an HTML file that loads correctly, and external files are imported at the end of the body section.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>TypeScript HTML App</title>
    <link rel="stylesheet" href="app.css" type="text/css" />
</head>
<body ng-app="app">
    <div ng-include="'movie.html'"></div>
    <!--Libraries-->
    <script src="Scripts/angular.min.js"></script>
    <script src="bundle.js"></script>
    <!--Controllers-->
    <script src="app/movieCtrl.js"></script>

</body>
</html>

The movie.html file includes:

<div ng-controller="MovieCtrl as vm"></div>

This is the content of movieCtrl file:

class MovieCtrl {

    // fields
    // constructor
    // properties
    // functions
}

angular
    .module('app')
    .controller('Movie',
    MovieCtrl);

Lastly, here is my app.ts file:

angular.module("app", []);

Any thoughts on why I am experiencing an error with the MovieCtrl?

Answer №1

// SOLUTION //

Upon meticulous examination of the PluralSight course, I finally pinpointed the mistake.

It turns out that in the movieCtrl file, I mistakenly defined the class as MovieCtrl, whereas in my module registration, I had inadvertently referred to it as Movie. The rectification process involved changing:

angular
    .module('app')
    .controller('Movie',
    MovieCtrl);

to

angular
    .module('app')
    .controller('MovieCtrl',
    MovieCtrl);

After making this correction, the error was successfully resolved.

Answer №2

The solution that worked for me was simply closing the Controller. It may be simple, but it fixes the issue.

Just remember to add either } or ) at the end:

.controller('UserCtrl', function (auth, $scope) { 
  $scope.auth = auth; 
  $scope.$watch('auth.profile.name', function(name) { 
    if (!name) { 
      return;
    } 
    $scope.message.text = 'Welcome ' + auth.profile.name + '!'; 
  });

So the final code snippet looks like this:

.controller('UserCtrl', function (auth, $scope) { 
  $scope.auth = auth; 
  $scope.$watch('auth.profile.name', function(name) { 
    if (!name) { 
      return;
    } 
    $scope.message.text = 'Welcome ' + auth.profile.name + '!'; 
  });
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Object autofill - Typescript utilizing Angular 5

I am working with an object called "features." Here is an example of the object: [{"_id":"5af03d95c4c18d16255b5ac7","name":"Feature 1","description":"<p>Description</p>\n","neworchange":"new","releaseId":"5aead2d6b28715733166e59a","produc ...

Evaluating Jasmine unit tests using promises with odata

I am new to working with Angular and unit testing in Angular. Our project involves using odata for performing CRUD actions on the database, so we have created a service for this purpose. Here is a snippet of our service code: function DatabaseService($htt ...

Is there a way to include a query directly as a string in Drivine and Neo4j, instead of using a separate file?

My current setup involves utilizing Drivine in conjunction with Neo4j. In the provided example, there is a demonstration of injecting a query sourced from a separate file. I am curious to learn how I can directly inline a query as a string instead? ...

Upon triggering an ajax post request, the HTML elements do not refresh as expected within an Angular environment

I am facing an issue where I make an AJAX POST request and it executes properly. After receiving the response, I need to make another AJAX GET request and assign the data to variables in the scope. The data is successfully assigned to the variables, but th ...

Error encountered with Protractor: 'TypeError: undefined is not a function'

I have explored various discussions on this particular error code. Nevertheless, I am finding it challenging to come across any solutions that are effective (or perhaps I am just not understanding them). While constructing a Protractor test for a webpage, ...

Tips for centering an Angular mat prefix next to a label in a form field

Hey everyone, I need some help with aligning the prefix for an input with the mat label. Can anyone suggest a way to adjust the mat prefix so that it lines up perfectly with the mat label? Any assistance or ideas would be greatly appreciated. Here is the ...

Angular 6 is showcasing dates in a quirky manner

In my Angular app, users can enter comments. The comments are displayed automatically with the date they were added. However, there is an odd behavior that I've noticed: When a user enters a comment, the time displayed is incorrect. (Check the ...

Tips for successfully passing function variables as parameters to Angular 2 HTTP subscribe callbacks

I attempted this.propositionService.addProposition(this.proposition) .subscribe(this.addSuccessCallback, this.addFailureCallback); The issue I am encountering is that both addSuccessCallback and addFailureCallback do not have acces ...

The recursive component is functional exclusively outside of its own scope

I'm facing an issue where my recursive component is not nesting itself properly. The problem arises when I try to use the Recursive component inside another Recursive component. Although the root is correctly inserted into the Recursive component fro ...

implementing dynamic visibility with ngIf directive in Angular

header.component.html <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <a href="#" class="navbar-brand">Recipe Book</a> </div> <div class="collapse na ...

Are there any solutions to refresh a page by clicking a button in next.js?

I am currently learning how to work with next.js and I'm facing an issue where I need to reload my page to make a new API request. As a beginner, I'm not sure how to achieve this. I've tried a few methods but none of them seem to work. Below ...

Angular.js page failing to reflect changes following Firebase request completion

myApp.controller('RegistrationController', ['$scope', function($scope) { var auth = firebase.database().ref(); // console.log("auth::"+auth); $scope.login = function() { $scope.message = "Welcome " + $scope.user.ema ...

Ways to launch angularjs project in production mode sans the use of npm start

Currently, I am in the process of developing a website with angularjs and have set up a local development server using npm. After moving my project directory to a public AFS directory, I encountered an issue where the npm dependencies are not functioning ...

Using Angular2's NgFor Directive in Components

I am faced with the challenge of converting a tree-like structure into a list by utilizing components and subcomponents in Angular 2. var data = [ {id:"1", children: [ {id:"2", children: [ {id: "3"}, {id: "3"}, {i ...

The $q.all promise is always fulfilled without being chained to then

While going through the feeds.forEach cycle, one of my promises ends up throwing an error. I suspect that this is happening because the line $rootScope.links = urls; inside the then block in LoadData is never being executed. How can I resolve this issue? ...

The VSCode's intellisense for Angular Material fails to function effectively

In the midst of my project on Angular version 13, I have successfully installed Angular Material using the command below: ng add @angular/material The package has been properly included in the node_modules folder. However, when working with TypeScript ...

An issue with the function in AngularJS has caused an undefined error

Encountering an error in my AngularJs app that states: TypeError: undefined is not a function at Scope.$rootScope.shareImageNow (index.controller.js:150) After researching on Google, I still couldn't figure out the reason behind this issue. co ...

Triggering ngSubmit function when button is clicked inside an Ionic alert

My ionic app is up and running, utilizing a template driven form in Angular to gather user input. I'm using ngSubmit to pass this data to my ts.file. My challenge lies in triggering the ngSubmit function through a 'No and save data' button w ...

Is it necessary to create a Node endpoint for each item in the array?

My current tech stack includes ExpressJs, NodeJs, and AngularJs. Imagine I have an array containing various objects representing grocery store accounts and the amounts owed to them by the bank. [{ account: 1, amount: 2.33 }, { account: 2, amount: 5.99 ...

How to Override Global CSS in a Freshly Created Angular Component

My CSS skills are a bit rusty and I need some assistance with a project I'm working on. The project includes multiple global CSS files that have properties defined for different tags, such as .btn. However, these global CSS files are causing conflicts ...