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

Tips on how to retrieve a nested promise

Within my JavaScript function, I am utilizing rest calls and the responses to construct the payload for subsequent calls. Included below is some pseudo code exemplifying my approach. Although my code is currently functional, I am unsure how to properly ret ...

Expanding the functionality of Promise to include progress updates

I had the idea to expand Promise by adding a 'progress' feature in order to track progress while using Promise for my asynchronous tasks. So, I created an extended version of Promise like this: class promisePro extends Promise { constructor ...

Matching a regular expression pattern at the beginning of a line for grouping strings

One of my tasks involves working with a markdown string that looks like this: var str = " # Title here Some body of text ## A subtitle ##There may be no space after the title hashtags Another body of text with a Twitter #hashtag in it"; My goal ...

Nested Angular Controller in a ng-repeat loop

I am attempting to utilize a nested controller within an ng-repeat in order to have the accordion panels on the page function in different scopes. However, I am encountering a problem where the code inside the nested controller is not being executed. I pla ...

Using AngularJS to differentiate between desktop and mobile devices, as well as determine if the connection is wifi or not

After some research on SO, I couldn't find similar Q&A about detecting connection types rather than just if a connection exists or not. The goal of my website is to automatically play a video clip based on the user's device and network statu ...

Issues with Sound Not Playing When Button is Clicked in Python Flask

My goal is to trigger an audio sound when the Delete button is clicked. I've created an external JavaScript file and successfully linked it with Flask. index.html <a href="/delete/{{todoitem.item_id}}" class="btn btn-danger" onclick="playDelSound ...

The function of slidetoggle is malfunctioning when clicked

I currently have two dynamic containers with one displaying grouped boxes, quantities, and totals, while the other shows detailed information. Both container values are added dynamically at runtime. By default, the grouping container is displayed on the p ...

Unable to swap out string with text box in TypeScript

I am trying to swap __ with a text box in Angular 2/4. Take a look at the example provided in the link below. https://stackblitz.com/edit/angular-ajkvyq?file=app%2Fapp.component.ts ...

Converting image bytes to base64 in React Native: A step-by-step guide

When requesting the product image from the backend, I want to show it to the user. The issue is: the API response contains a PNG image if the product has an image, but returns a (204 NO Content) if the product does not have an image. So, I need to display ...

How come my keyframes animation isn't functioning properly on my div element?

I am currently designing a custom animation for a checkers game. My goal is to create an effect where the checker piece moves slowly to its next spot on the board. Below is the CSS code I have used: @keyframes animateCheckerPiece { 0% {top: ...

Validation messages in an Angular application using Typescript are failing to display or disappear sporadically when applied to an HTML form that has

I am currently working on a simple app that retrieves website content from a CMS [Umbraco]. After making an Ajax call, the form comes back to me as plain HTML. I then append the form to the page and use the Angular $compile service to compile the result. T ...

An uncaught SyntaxError occurred due to an omission of a closing parenthesis after the argument list in code that is otherwise

I have a Javascript code that sends an Ajax request and upon receiving the data, it creates an "a" tag with an onclick event that triggers another method along with a parameter. Below is the implementation: function loadHistory() { var detailsForGe ...

Browser refresh not triggering view update in Rails and ReactJS application

Recently, I integrated Reactjs into my Rails application. Strangely, when I modify the content of a .jsx file and reload it with different text, such as changing from <h1>Hello<h1/> to <h1> Hello again<h1/>, the browser fails to res ...

Discovering if the array data is already present in Angular can be accomplished by using specific methods

Here is the snippet of code: data = [ { 'id': 'asdjxv', 'username': 'emma', }, { 'id': 'asqweja', 'username': 'adam', }, { ...

Custom AngularJS directive for ensuring the selection of a required HTML element

Today brings another question as I continue working on my web application. I've encountered an issue with the 'required' attribute not being widely supported in major browsers. The attribute only works if the first option value is empty, whi ...

Navigate to a new webpage using a string of characters as a legitimate web address

When a user performs a search, I pass the search term as a string to direct them to a new page. How can I create a valid URL (e.g., remove spaces from the string)? It's crucial that the next page can recognize where any spaces were in the search word ...

Troubleshooting: Vue.js Component template not displaying items with v-for loop

I recently implemented a method that calls an AJAX request to my API and the response that it returns is being assigned to an array. In the template section, I am using the v-for directive to display the data. Surprisingly, it only renders once after I mak ...

Achieving nested views functionality with various modules in AngularJS and ui-router

I am currently working on a complex AngularJS application with a large module that I am looking to break down into smaller, more manageable modules. Our main page consists of several ui-views for menu, footer, content, sidebar, etc. Each $stateProvider.st ...

Ways to conceal the picture

Check out the JSfiddle link here for the full code. I'm having trouble with my slider as the last picture keeps collapsing underneath and is not hidden as it should be. I suspect this issue is causing the slider to malfunction. HTML <div class=" ...

Sending a $.ajax post request transforming into a get

Struggling to understand the behavior of my jquery ajax request, I've hit a roadblock. function newContact() { $.ajax({ type: 'POST', contentType: 'application/json', // url: ...