What is causing my basic angularjs to not function properly?

In my angularjs test instance, I am using a global variable but the alert tag is not functioning as expected. This code snippet is from my first example on codeschool.

Any thoughts on why the alert is not running?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
</head>
<body ng-controller="StoreController">


    <script type="text/javascript">
        function StoreController() {

            alert("hello");
        }

    </script>
</body>
</html>

Answer №1

Don't forget to include the ng-app attribute:

<html ng-app xmlns="http://www.w3.org/1999/xhtml">

@PSL mentioned in the comments that global controllers won't work from angular 1.3 onwards. You need to specify the controller for your application.

Name your application within ng-app, like this, on the html tag:

<html ng-app="myapp" xmlns="http://www.w3.org/1999/xhtml">

Then, define your module and controller in JavaScript:

var myapp = angular.module("myapp", []);

myapp.controller('StoreController', ['$scope', 
    function ($scope) {
        alert("hello");
    }
]);

Answer №2

Don't forget to add the necessary ng-app line in order to properly bootstrap your application. Here is a sample code snippet that you can use:

<body ng-app="app">
    <div ng-controller="StoreController">Hello World</div>
    <script type="text/javascript">
        /* Make sure to include this line */
        angular.module("app", []);
        angular.module("app").controller("StoreController", [
            function() {
                alert("Hello");
            }
        ]);    

        /* Keep in mind that global controllers may not work with Angular 1.3 
        function StoreController() {

            alert("hello");
        }*/
    </script>
</body>

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

What are the steps to retrieve marker data from a MySQL database and showcase them dynamically on a Google Map using AJAX? Addressing time constraints

Utilizing a combination of JavaScript, Google Maps API, PHP, and MySQL, my app is designed to extract a list of markers from a MySQL database, export them to a myXML.xml file, and then load these markers onto the map each time it loads. <!DOCTYPE html& ...

Get the azure blob storage file in angular by downloading it

Can anyone provide guidance on how to download a wav file from Azure Blob storage using Angular for the purpose of playing it with wavesurfer.js? ...

Skipping an iteration in ng-repeat in Angular 1.0.8: A simple guide

Is it possible to skip a particular iteration in ng-repeat without modifying the underlying array/object being iterated over? Consider the following example: var steps = [ { enabled: true }, { enabled: true }, { ...

How can I generate a hyperlink for a specific directory on Github Pages?

If I have a repository with one file and one folder, as listed below: index.html folder The folder contains another file named: work.html My goal is to access the folder website using only the link: username.github.io/repositoryname/folder Instead ...

React Native, state values are stagnant

I created an edit screen where I am attempting to update the post value through navigation v4 using getParams and setParams. However, when I modify the old value and click the save button, it does not update and no error is displayed. The old values still ...

Utilizing Vuetify 2 skeleton-loader to customize loading states through Vuex store manipulation

Utilizing the Vuetify v-skeleton-loader component to wrap a v-data-table component. The server-side pagination and sorting in the data-table component are set up. To enable server-side pagination, the documentation recommends monitoring the options objec ...

Is it possible for the ionic directive "ion-nav-view" (ui-router) to be compatible with ngRoute?

I have successfully merged the Angularfire-Seed with a demo Ionic App. The integration has been smooth so far. To navigate between different views, I am looking to incorporate Ionic's functionality: // requires ui-router <ion-nav-view></ion ...

Obtain user input and extract the name using jQuery's serialization function

Trying to extract user input from a dynamic form using jquery serialize. The structure of my form is as follows: <form id="lookUpForm"> <input name="q" id="websterInput" /> <button onclick="webster(); return ...

Guide to effectively utilizing jQuery Deferred within a loop

I have been working on a function that needs to loop through a dataset, updating values as it goes along using data from an asynchronous function. It is crucial for me to know when this function finishes running the loop and completes all updates. Here is ...

Tips for disabling automatic scrolling when a browser is reloaded using JavaScript

In today's world, most browsers automatically adjust the window scroll position upon page refresh. While this is generally a helpful feature, I find myself in a situation where I need to reload a page and direct focus to a different part of the page t ...

When the JSON object is transferred to the node server, it undergoes modifications

With the following code snippet, I have managed to develop a function that generates JSON data and transmits it to server.js (my node server). function deleteEmail(i) { emailObj.splice(i, 1); var general = {}; var table = [] general.table ...

Ways to deactivate the three-way data binding feature in Firebase

Currently using AngularJS 1.3 in combination with Angularfire and Firebase has been a successful experience for me. I have created some factories to manage the firebase object, and the three-way data binding feature is truly impressive. However, as I plan ...

The onprogress event for the XMLHttpRequest object threw an error due to an Uncaught SyntaxError, indicating

I have implemented an ajax function successfully However, I am facing an issue where when using onprogress, I sometimes receive incomplete HTML response and the console displays Uncaught SyntaxError: Invalid or unexpected token but the function still cont ...

Navigate to a different page in Vue3 after completing the file download process

After receiving a link from the server to download the file, the user initiates the function to begin downloading. Once the file has finished downloading, the user will be redirected to the main page. <script setup lang="ts"> const goToMai ...

Loading custom places in ArcGIS from a file or database

Hey there, I was wondering about loading custom places with Arcgis similar to Google maps loading from a .xml file. I noticed that Arcgis uses examples saved in .json format, but when I tried putting the example .json on my local server it wouldn't lo ...

Unable to designate decimal value as the default in DropdownListFor in ASP.NET Core when utilizing JavaScript

Everything appears to be functioning properly, except when dealing with decimal values in the following code snippet: $('select#listspec_0__qty option[value = 105.3]').attr("selected", true); ...

Enhancing user experience: Implementing specific actions when reconnecting with Socket.io after a disconnection

I am working on a game using node.js and socket.io. The code I have written is quite simple. The game starts animating as soon as it connects to the server and continues to do so. My main concern is ensuring that the game handles network and server disco ...

Attempting to locate an element within the DOM using TypeScript

I am completely new to TypeScript. I have been attempting to locate an element using a selector, but no matter what I tried, the findElement() method always returns undefined. Can someone please point out where my mistake might be? Any assistance would b ...

Do the dependencies in the devDependencies section impact the overall size of the

My search for a definitive answer to this question was fruitless. Are the packages I include as devDependencies included in the final production JS bundle, impacting its size? Or does only the dependencies list affect the bundle's content? ...

Implementing ngAnimate within AngularJS 1.3 allows for the utilization of animate.css animations, thereby offering a diverse range

I am currently investigating the discrepancies between the animation behavior in Firefox and Chrome/IE. It appears that when displaying a message, IE/Chrome exhibit a bounce effect. The source code resembles the following: <!DOCTYPE html> <html ...