AngularJS $scope variable issue

After searching online, I found this code snippet that I tried to implement, but unfortunately, it's not displaying any data.

Below is the HTML code:

<html data-ng-app="">
<head>
    <title>Example 4: Using AngularJS Directives and DataBindings</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="Styles/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="container" ng-controller="SimpleController">            
        <h1>AngularJS Second Program</h1>
        <br/>
        Name:
        <br />
        <input type="text" data-ng-model="name" /><br/>
        <br>
        <ul>
            <li data-ng-repeat="cust in customers | filter:name | orderBy:'city'">{{cust.name | uppercase}} - {{cust.city | lowercase}}</li>
        </ul>
    </div>
    <script type="text/javascript" src="Scripts/angular.min.js"></script>
    <script>
        function SimpleController($scope)
        {
            $scope.customers = [
                {name:'John Doe',city:'New York'},
                {name:'John Smith',city:'Pheonix'},
                {name:'Jane Doe',city:'Chicago'}
                ];
        }
    </script>
    <script src="Styles/jquery.min.js"></script>
    <script src="Styles/js/bootstrap.min.js"></script>
</body>

I also added Bootstrap for styling purposes.

A working example I referred to earlier looked like this:

<html data-ng-app="">
<head>
    <title>Example 3: Using AngularJS Directives and DataBindings</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="Styles/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body data-ng-init="customers=[{name:'John Doe',city:'New York'},{name:'John Smith',city:'Pheonix'},{name:'Jane Doe',city:'Chicago'}]">
    <div class="container">         
        <h1>AngularJS Second Program</h1>
        <br/>
        Name:
        <br />
        <input type="text" ng-model="name" /><br/>
        <br>
        <ul class="list-group">
            <li class="list-item" data-ng-repeat="cust in customers | filter:name | orderBy:'city'">{{cust.name | uppercase}} - {{cust.city | lowercase}}</li>
        </ul>
    </div>
    <script type="text/javascript" src="Scripts/angular.min.js"></script>
    <script src="Styles/jquery.min.js"></script>
    <script src="Styles/js/bootstrap.min.js"></script>
</body>

This is only my fourth attempt at using AngularJS, so I'm pretty clueless about what went wrong. Any help would be greatly appreciated. Thank you!

Answer №1

It appears to be running on angular version 1.2 and must be initialized with an app:

angular.module('myApp', []); // feel free to give it any name you'd like

Next, include it in your ng-app attribute:

<html data-ng-app="myApp">

Then, you can create your controller by creating a new module:

angular.module('MyApp.Controllers', []) // naming is flexible
    .controller('CustomController', ['$scope', function($scope){
        // implement your controller logic here
    }]);

Add this to the app as well:

angular.module('myApp', ['MyApp.Controllers']);

Alternatively, you can add it directly to your app:

angular.module('myApp').controller('CustomController', ['$scope', function(){
    // NOTE: When using the existing app module, do not include [], just refer back to it for further modifications.
}]);

Answer №2

It seems like there may be an issue with loading or corruption in your angular.js file. You can try using the CDN link as it should work properly.

Answer №3

To implement this, ensure that you initialize your Angular app correctly

<html data-ng-app="myapp">
<head>
    <title>Example 4: Using AngularJS Directives and DataBindings</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="Styles/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body>
    <div class="container" ng-controller="SimpleController">
        <h1>AngularJS Second Program</h1>
        <br/>
        Name:
        <br />
        <input type="text" data-ng-model="name" /><br/>
        <br>
        <ul>
            <li data-ng-repeat="cust in customers | filter:name | orderBy:'city'">{{cust.name | uppercase}} - {{cust.city | lowercase}}</li>
        </ul>
    </div>
    <script type="text/javascript" src="Scripts/angular.min.js"></script>
    <script>

    angular.module('myapp', []).controller('SimpleController', ['$scope',
        function($scope) {
            $scope.customers = [
                {name:'John Doe',city:'New York'},
                {name:'John Smith',city:'Pheonix'},
                {name:'Jane Doe',city:'Chicago'}
                ];

        }
    ]);
    </script>
    <script src="Styles/jquery.min.js"></script>
    <script src="Styles/js/bootstrap.min.js"></script>
</body>

Answer №4

It appears that SimpleController is not defined in the application module. Consider renaming your app to 'myApp' and defining your controller as shown below:

angular.module('F1FeederApp.controllers', [])
   .controller('simpleController', function($scope) { ...

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

Issues with loading JSON data through JQuery

I am completely new to the process of loading JSON text using JavaScript or JQuery. JSON is a new concept for me as well. Currently, I have PHP providing me with some JSON text containing images stored on my server in the following format: [ [{ ...

Creating a sidebar that remains fixed in place even as the page is scrolled down can be achieved by using CSS and positioning properties

I am looking to create a website with a specific layout design as shown in this image: https://i.stack.imgur.com/ndKcz.png The main focus is on making the sidebar (F/T container) function as a social network link, sticking to the right side of the page ev ...

Incorporating a personalized image to create custom icons on the Material UI bottom navigation bar

Is it possible to replace the default icon with a custom image in material ui's BottomNavigation component? I'm curious if Material UI supports this feature. If you'd like to take a closer look, here is the link to the codesandbox. ...

Is There a Way to Abandon a route and Exit in Express during a single request?

In order to ensure proper access control for the application I was developing, I structured my routing system to cascade down based on user permissions. While this approach made sense from a logical standpoint, I encountered difficulties in implementing it ...

What is the best way to update the state of a particular slider component?

When trying to update the value of a specific slider during the onChange event, I noticed that it was affecting all sliders instead. Is there a way to target and set the state of only one slider during this event? Here's what I've attempted so f ...

Is it possible to iterate over a non-reactive array in a Vue template without having to store it in a data property or computed property?

Presented below is a static array data: const colors = ["Red", "Blue", "Green"]; To display these values in my markup, I can use the following method in React: import React from "react"; // Static array of colors const colors = ["Red", "Blue", "Green"] ...

Trigger a fire event upon entering a page containing an anchor

My query is similar to this particular one, with slight differences. I am working on a page that includes an anchor, such as page.html#video1. Within this page, there are multiple sections identified by ids like #video1, #video2. Each section comprises of ...

Leveraging TipTap.dev for building a joint editing platform -

I have integrated the collaboration feature from tiptap.dev into my NextJS application. Initially, I used their CLI command for the Hocuspocus server which worked well on port 1234 locally and synchronized text editing across browsers seamlessly. However, ...

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 ...

Encountering a 404 error when trying to access the rxjs node_module

While attempting to compile an angular2 application, I encountered the following issue: Error: XHR error (404 Not Found) loading http://localhost:3000/node_modules/rxjs(…) systemjs.config.js (function(global) { // map tells the System loader whe ...

The angular.js routing feature seems to be malfunctioning as the view is not loading properly

I'm facing an issue with my angularjs routing as the view is not rendering when a link is clicked. Surprisingly, there aren't any errors showing up in the console. Here's how I have set it up: (function(){ var app = angular.mod ...

Changing the designated materialUI class

Within the project, I am utilizing this theme: export const theme = createMuiTheme({ ...defaultThemeConfig, overrides: { ...defaultThemeConfig.overrides, MuiListItem: { root: { '&:nth-child(odd)': { backgro ...

Utilizing the power of Angular 4 in combination with mailgun

I need assistance with setting up an email form on my website using Angular 4 and Mailgun as the mail service. I have a method in my mail service file to send messages, but I keep encountering a Bad Request error stating that 'from' is not presen ...

The function Server.listeners is not recognized by the system

Currently, I am following a tutorial on websockets to understand how to incorporate Socket.IO into my Angular project. Despite meticulously adhering to the instructions provided, I encountered an error when attempting to run my websockets server project: ...

Choosing the subsequent div after the current one using Jquery

I am currently in the process of creating a drop-down menu button that, when clicked, reveals the previously hidden div underneath it. The code is functioning properly without the use of $(this).next, but it is displaying all divs with the class .menudrop ...

In JavaScript, use the href property to redirect to an external URL and then automatically scroll to a specific class on the page

I have a specific scenario where I need to create a link that redirects to an external website, of which I do not own. However, I am aware that there is a div with a particular class located at the bottom of their page, linking to an article. My goal is to ...

Having trouble accessing variable values within the nth-child selector in JavaScript

I am attempting to utilize the value of a variable within the element selector p:nth-child(0). Instead of hardcoding the number as 0, I want to dynamically assign the value of a variable. In this case, the variable is represented by i in a for loop. Howev ...

React's `setState` function seems to be failing to hold onto

Recently, I've been challenged with creating an infinite scroll loader component. However, I'm facing a peculiar issue where my 'items' array is constantly resetting to [] (empty) instead of appending the new results as intended. A cou ...

What is the impact of sending a JavaScript request to a Rails method every 15 seconds in terms of efficiency?

I have a method that scans for notifications and initiates a js.erb in response. Here is the JavaScript code triggering this method: setInterval(function () { $.ajax({ url: "http://localhost:3000/checkNotification", type: "GET" }); }, 15000); ...

Changing the old state in React js: A step-by-step guide

I have a collection of Faq elements. Clicking on a question should display the answer for that specific question while hiding all other answers. The issue I'm facing is that even though it displays the answer for the clicked question, it fails to hi ...