Creating a basic Angular 1 application from scratch

Currently, I am in the process of setting up a small application with Angular 1 to enhance my skills (yes, we are still using version 1 at my workplace and I want to familiarize myself with it).

Most of the application is working fine, but as soon as I introduce another controller, everything seems to fall apart. This is how my project is structured:

app.js:

angular.module('app', [  
    'app.controllers',
    'ds.clock'
]);

clockController.js:

angular.module('app.controllers', []).controller('clockController', 
    function($scope) {
        "use strict";

        $scope.Name = "Angular";
});

index.html (shortened):

<body ng-app="app"> 


    <div ng-controller="clockController" style="float:right;width:20%x;margin:auto">

{{Name}}

    <div>
        <div><ds-widget-clock show-analog></ds-widget-clock></div>
    </div>

The clocks are displaying correctly along with the 'Name' property on the scope.

However, when I try to include an additional controller:

tickerController.js

angular.module('app.controllers').controller('tickerController', 
    function($scope) {
        'use strict';

        $scope.Name = 'ticker'; });

I have also linked it in the <head>:

<script src="js/app.js"></script>
<script src="js/clockController.js"></script>
<script src="js/tickerController.js"></script>

Additionally, I added a new div in the index.html

  <div ng-controller="tickerController" >

        {{ $scope.Name }}

    </div>

    <div ng-controller="clockController"........

Upon doing this, I encountered the following error:

angular.js:14700 Error: [$controller:ctrlreg] The controller with the name 'clockController' is not registered.

As a result, most of my page remains blank.

Any suggestions on how to proceed from here?

Answer №1

It seems like your approach may be incorrect. Try creating an additional module, registering the controller with that module, and including it as part of the main module. This should resolve the issue. Alternatively, you can also use the following solution:

angular.module('app.controllers', [app.clockController])
.controller('clockController', function($scope) {
    "use strict";

    $scope.Name = "Angular";
});

This alternative method should also work.

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

Unable to access API endpoint for retrieving items as described in the Express/React tutorial on Pluralsight

Currently, I am going through a tutorial on React, Express, and FLUX. Unfortunately, I have encountered a roadblock in the form of a CANNOT GET error when trying to access my API data. https://i.stack.imgur.com/RbUzf.png In the server file under Routes & ...

CORS OPTIONS request encountered while attempting to upload a file using $http in AngularJS

I am currently facing challenges in implementing a file upload system between my client-side AngularJS app and server. The issue seems to stem from the preflight OPTIONS request sent by Chrome. Testing the route with Postman yields successful results, as ...

Implementing HTMX with just one HTML page: A step-by-step guide

Just starting out with HTMX and created a sample: <!DOCTYPE html> <html> <head> </head> <body> <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfema ...

When using threejs, the color set for setClearColor is supposed to be white. However, when calling an external HTML file, it unexpectedly renders as

When I call an external HTML file in Three.js, the value for setClearColor is white but it renders as black. How can I solve this issue? Click here to view the image Here are the codes from the external file: <div id="3d-modal"></div> <sc ...

Choosing the Right Project for Developing HTML/Javascript Applications in Eclipse

Whenever I attempt to build a webpage using eclipse, I am presented with two choices: -- A Javascript project -- A Static web project If I opt for the former, setting up run to open a web browser can be quite challenging. If I decide on the latter ...

Looking to transition from Angular 1.5x to ReactJS, what is the most effective method for conducting A/B tests and exploring different views?

As I transition part of the UI code to ReactJS, I am considering A/B testing my app for instrumentation. I have looked into Intuit's Wasabi as a potential framework but found its setup process in production to be cumbersome. I am seeking an alternativ ...

Disabling the ng-click event in a confirmation popup button: a step-by-step guide

Is there a way to prevent ng-click event from triggering in a confirmation popup button? 1[Screen-Shot] ...

Sending values to URL using the <a> tag in HTML

I currently have the selected language stored in a variable (var lan= urlParam('language')). I am trying to pass this language as a parameter (without PHP) in a URL within an "a" tag, like so: <a href="http://hotelscombined.sitewish.gr/HotelN ...

Utilizing dynamic components in React JS

I'm a beginner in React JS and I'm facing an issue where I'm trying to call a React component from an HTML string that is being generated by another JavaScript class. However, the component is not rendering on the screen. class Form extends ...

Move images horizontally next to the height of an element

I am attempting to increase the top attribute in relation to the element it is currently adjacent to. The element should only scroll directly next to the other element and cease when it surpasses the height of that element. My Goal: I want the image to re ...

Dragging additional events into FullCalendar is disabled after applying a filter to external events

I am encountering an issue while attempting to drag events from the external events box to the fullcalendar. To replicate the problem, you can visit this CodePen: Initially, dragging an external event into the calendar works smoothly. However, after appl ...

Attempting to retrieve information from my MongoDB database and populate it into a <table> structure on a web page

My objective is to extract data from a MongoDB database and display it in an HTML table. Specifically, I am trying to retrieve information from the hangman database's players collection, which contains fields for name and score. Can anyone help me ide ...

The issue with implementing simple getElementsByClassName JavaScript in the footer of a WordPress site persists

I am facing an issue with a 1-liner JavaScript code in the footer where getElementsByClassName function doesn't seem to work for tweaking style attributes. The text "Hello World" is displaying fine, so I suspect it might be a syntax error? Here' ...

Execute JavaScript/AJAX solely upon the selection of a button

Currently, my script is being executed immediately after the page loads and then again after a button click. Is there any way to modify it so that the script waits until I click the button before running? As far as I understand, this code runs asynchronous ...

Error: Attempting to access the 'body' property of an undefined object following a POST request in a MEAN application

Currently, I am attempting to send POST data from AngularJS to Express. My approach involves using curl to transmit the POST content. After sending the data and receiving a 200 response, I encounter an issue when trying to access the data through body-par ...

What is the proper way to execute a method while routing to a template in AngularJS?

Looking for help on how to execute a method from a service while navigating to a template in AngularJS? wikiApp.config(['$routeProvider', 'faqService', 'newsService', function($routeProvider, faqService, newsService) { $ ...

React Error: Unable to iterate over this.state.descriptions

Currently facing an issue while trying to resolve this error https://i.stack.imgur.com/BZ304.png The goal is to generate an automated form using the following function: let descriptionsForm = ( <form> {this.state.descriptions.map((d ...

Internet Explorer IE 11 encounters an "Error: Object does not support property or method" issue

Recently, I started using the jquery circleChart.min.js plugin for creating a circle chart. It's been working perfectly on all browsers except for Internet Explorer 11 (IE11). I keep getting an error message when trying to run it in IE11. Can anyone h ...

Cleaning up React async functions using hooks for fetching data

This code snippet is from a functional component. Within this component, there is a submit() function that handles form submission: async function handleSubmit(event) { event.preventDefault(); try { let resp = await fetch("FOOBAR/BAX", { ...

Adjusting the label on the Formik toggler

I have successfully implemented a switcher using Formik that changes the label accordingly with a boolean value. However, I now need to make the label reflect a string value instead of just "true/false." For example, I want it to display "Text1/Text2" inst ...