Encountering Errors with Multiple Controllers in my Angular JS App

As a beginner in Angular JS, I understand that the declaration below is necessary in the js file to make angular JS Controller work:

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

app.controller('ctrlOne', function(){
});

However, I recently came across some code on the internet where the declaration for the app and controllers was not made as above. Instead, only controller functions were defined, and it worked fine there but not in my case.

https://i.sstatic.net/OgMfM.png

https://i.sstatic.net/Ogspd.png

function ctrlOne($scope){

};

function ctrlTwo($scope){

};

Take a look at my code and output below. Please let me know if you spot any errors.

<div ng-app>
    <input type="text" ng-model="data.message" />
    <h1>{{ data.message }}</h1>

    <div ng-controller="ctrlOne">
        <input type="text" ng-model="data.message" />
        <h1>{{ data.message }}</h1>
    </div>

    <div ng-controller="ctrlTwo">
        <input type="text" ng-model="data.message" />
        <h1>{{ data.message }}</h1>
    </div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="App.js"></script>

App.js:

function ctrlOne($scope){

};

function ctrlTwo($scope){

};

https://i.sstatic.net/CT9l6.png

I would appreciate any help in figuring out where I went wrong. All advice is welcomed.

Check out the JSFIDDLE Link for the above coding here

https://i.sstatic.net/jIrmM.png https://i.sstatic.net/x81fS.png

Answer №1

Modify your HTML to include the module name myApp as shown below:

<div ng-app="myApp">
...
  <div ng-controller="ctrlOne">
      <input type="text" ng-model="data.message" />
      <h1>{{ data.message }}</h1>
  </div>
...
</div>

In order for Angular to recognize the root module, it needs to know its name. The ng-app directive specifies this:

Use this directive to auto-bootstrap an AngularJS application. The ngApp directive designates the root element of the application and is typically placed near the root element of the page - e.g. on the <body> or <html> tags.

Answer №2

When it comes to starting an Angular app, there are a couple of options available. One approach is to declare the ng-app directive in your HTML code, while another option is to bootstrap the Angular app manually. It's important to identify where exactly the app initialization is taking place. For example, if you have ng-app='' in your HTML, make sure you specify the app name that should be associated with it.

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

Error: The hyperlink in the response from the Ajax request is not functioning as expected

I've exhausted all the suggestions for fixing this issue, but nothing has worked so far. Currently, my setup involves making an AJAX call to a PHP page called livesearch.php from the index page in order to retrieve live search results. <html> ...

Ways to make a jQuery function more concise

I need help with optimizing this jQuery function. It's repetitive and I want to find a way to shorten it while achieving the same result. Can someone assist me in streamlining this code? $(".c1").delay(5000).fadeOut("slow", function() { $("#phone ...

Tips on aligning a span element at the center of an image without losing its mouseover

<div class="pic"> <img src="image.jpg" height="250"/> <span class="text" style="display:none">text here</span> </div> <scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </scrip ...

Angular's ng-bind-html directive for embedding SVG content does not function properly in Internet Explorer

We're in the process of developing an application using angular.js, which involves a large svg-image containing numerous other svg-images and shapes. One major issue we've encountered is that Internet Explorer (even version 11) is refusing to bi ...

The method "_super" is not supported by the object in igGrid

When using infragistics and igGrid in my application, I encountered a javascript error. The error message reads: "Object doesn't support property or method "_super" Although I understand how to resolve this issue, I have decided to provide a fake ...

HTML/JavaScript - Ways to show text entered into an input field as HTML code

One dilemma I'm facing involves a textarea element on my website where users input HTML code. My goal is to showcase this entered HTML code in a different section of the webpage. How should I approach this challenge? The desired outcome is similar to ...

Ways to display the page's content within a div container utilizing Jquery

I am attempting to display the content of a URL page (such as ) within a <div> using JQuery, but so far I have been unsuccessful. Here is an example of what I am trying to achieve: <div id="contUrl"> .. content of google.fr page </div> ...

What causes the function execution to not be delayed by setTimeout?

function attemptDownloadingWebsite(link) { iframe = document.getElementById('downloadIFrame'); iframe.src = link; setTimeout(removeFile(link), 25000); } This is the remove file function: function removeFile(link){ $.ajax ...

Having trouble invoking the .js file with form POST

I've encountered a problem with my code that is technically "working," but it's not functioning as intended. Within the header of my page, I have the following code snippet: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jqu ...

Error message "Uncaught in promise" is being triggered by the calendar function within the Ionic

Can someone assist me in creating a calendar feature for my app? My concept involves a button with text that, when clicked by the user, opens a calendar. However, I am encountering an error message: ERROR Error: Uncaught (in promise): TypeError: Cannot set ...

The div's width shifts upon reaching the top of the page

Creating a blog site example and trying to lock the position of a div once it reaches the top of the page. Utilizing materialize.css for this task. <div class="row"> <div class="col m8 s12"> <!-- content goes here --> < ...

`Why setRequestHeader is essential for Ajax and XMLHttpRequest`

Should I ever manually specify the setRequestHeader as 'application/x-www-form-urlencoded' for an ajax POST request? Is it necessary to manually define the setRequestHeader as 'multipart/form-data' when uploading files via ajax? Do XMLH ...

Changing webpage content without using asynchronous methods once authentication has been completed using Google Firebase

My goal is to create a website where users can sign in with Google by clicking a button, and then be redirected to a new HTML page. However, I am facing an issue where the Google sign-in window pops up briefly and closes immediately, causing the HTML page ...

Unable to display cgi script results on webpage <div> with javascript

My goal is to display the output of a CGI script within a <div> on an HTML page using the following code: HTML <b>Already Loaded Data</b> <div id="result"></div> JS <script type="text/javascript" > var fdate=' ...

Struggling with navigating segmented dropdown buttons

I was following a tutorial on Udemy to learn about inputs and dropdown buttons for bootstrap. Everything was going well until I attempted to create a simple selection of albums for one of my favorite artists. The buttons kept separating when I tried to cen ...

Using jQuery, remove any white spaces in a textbox that are copied and pasted

There is a textbox for entering order IDs, consisting of 7 digits. Often, when copying and pasting from an email, extra white spaces are unintentionally included leading to validation errors. I am looking for a jQuery script to be implemented in my Layout ...

The method of reading a unique array of objects for each radio button

Currently, I am facing an issue when trying to retrieve unique elements for each radio button from the database. The data structure and information obtained from the database are as follows: { FormularID: 182, CampaignID: 14, FormLabel: & ...

Sending a message through Discord.JS to a designated channel

Recently diving into Discord.JS, I am struggling to understand how to make my bot send a message to the General Chat when a new user joins. Many examples I've come across suggest using the following code: const channel = client.channels.cache.find(ch ...

Verify if there is a date present within the JSON entity

I have a PHP array containing date strings and I want to validate them using a native HTML5 date input element. To achieve this, I have converted the date array into a JSON object for use with JavaScript: <script> var date_array = <?php echo json ...

Jquery is unable to detect duplicate class names

if ($(".event_list")[0]){ if($(".event_list").find(".type").length == 0 && $(".event_list").find(".sold").length == 0) { $(".event_list").click(); } } else { $('#something').click(function() { location.reload(); }); } I attemp ...