What could be the issue with this piece of Angular code?

Just starting to dive into AngularJS and following a tutorial on YouTube. I've copied the exact code from the instructor, but for some reason it's not working for me. Apologies for the newbie question, but I'm genuinely surprised that the code isn't functioning as expected. Thank you in advance for any help!

<body data-ng-app="">
<div data-ng-controller="SimpleController">
    <ul>
        <li data-ng-repeat="cust in Customers">
            {{cust.Name}}
        </li>
    </ul>
</div>
<script src="../Scripts/angular.js"></script>
<script>
    function SimpleController($scope) {
        $scope.Customers = [
            { Name: "Chanchal", City: "Dhaka" },
            { Name: "Masud", City: "Jessore" },
            { Name: "Humayun", City: "Barisal" }
        ];
    }
</script>

Answer №1

To ensure compatibility, please make sure you are using an angular version that is below 1.3. You can verify this information by checking the link provided.

<!DOCTYPE html>
<html>

<head>
  <script  src="https://code.angularjs.org/1.2.0/angular.js"></script>
</head>

<body data-ng-app>
  <div data-ng-controller="SimpleController">
    <ul>
      <li data-ng-repeat="cust in Customers">
        {{cust.Name}}
      </li>
    </ul>
  </div>
 
<script>

function SimpleController($scope) { $scope.Customers = [ { Name: "Chanchal", City: "Dhaka" }, { Name: "Masud", City: "Jessore" }, { Name: "Humayun", City: "Barisal" } ]; }
</script>
</body>

</html>

Answer №2

In Angular, it is generally not recommended to use global controllers unless you explicitly allow for them using controllerProvider.

 function AdvancedController($scope) {
        $scope.Users = [
            { Name: "Sarah", City: "New York" },
            { Name: "Michael", City: "Los Angeles" },
            { Name: "Emma", City: "Chicago" }
        ];
    }

AdvancedController.$inject = ['$scope'];
angular.module('myApp', []).controller('AdvancedController', AdvancedController);

In your HTML file, include the following:

<body  ng-app="myApp">

Answer №3

To begin your angular application, you must first bootstrap it.

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

<script>
  var myApp = angular.module('myApp',[]);

  function SimpleController($scope) {
    $scope.Customers = [
        { Name: "Chanchal", City: "Dhaka" },
        { Name: "Masud", City: "Jessore" },
        { Name: "Humayun", City: "Barisal" }
    ];
  }
</script>

In addition,

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

Check out this JSfiddle link for more information!

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

Transferring form data to JavaScript using PHP with jQuery's AJAX functionality

I am currently in the process of submitting a form using AJAX and incorporating jQuery. I am trying to figure out how to pass form arrays to it. For instance, my HTML structure might look like this: <input id="standard_field" type="text" name="standar ...

How to utilize the translateY() method to seamlessly shift concealed elements with no empty space

My goal is to create an interactive animation that reveals a specific section when a user clicks on an element. Initially, this section is hidden from view using the translateY(-700px) property. However, I have noticed that applying a transition effect to ...

What is the best way to save inputted names as properties of an object and assign the corresponding input values as the values of those properties?

I am searching for a way to dynamically build an object where each property corresponds to the name of an input field and the value of that property is the input's value. Here is the HTML structure: <form> <fieldset> ...

What is the best way to hide the menu bar on a particular window using electron?

In my application, there is a menu that opens the document properties window when clicked. However, I am facing an issue where the application menu gets inherited by the document properties window itself, allowing you to open another document properties wi ...

Creating a Copy to Clipboard feature in React can be achieved by transferring data from an h1 tag to an input field

I'm trying to extract data from an API into an h1 tag in my app. Is there a way for me to easily copy this data and paste it into an input field? Any help would be greatly appreciated. Thanks! ...

Trigger an analytics event from a background chrome extension

I've encountered a challenge with sending an event to Google Analytics from background.js, which is the background script of my Chrome extension. This is the code snippet I added to my background.js file: var _gaq = _gaq || []; _gaq.push(['_set ...

Tips for deactivating trackball rotation events and triggering a custom rotation event while adjusting the rotation speed within the custom event

In an attempt to disable the trackball control rotate event and trigger a custom rotate event, I have encountered some challenges. While setting controls.enableRotate = false; works for OrbitControl, it does not completely disable the trackball control r ...

Issue found when utilizing ngRepeat. Checkbox and radio button statuses being incorrectly set

In the controller of my component, I have an array of users that I attempted to display using the ngRepeat directive. However, there are some bugs in the output: Only the second user should have the active property set to true. The second radio button sh ...

Utilizing Javascript or XUL windows without the use of iframes offer

I'm in the process of creating a multitab website for my bookmarks, but I've run into some issues. Here is the JavaScript version of what I'm trying to achieve: Unfortunately, there are obstacles with this method. The websites in the tabs ...

What is the process for configuring a server page within create-react-app for sending API requests?

As I dive into learning React using create-react-app, my latest project involves making an API request to an external source. Initially, I included this request in the front end of the app as I was working on building it out. However, now I realize the imp ...

The menu is loaded dynamically by Ajax from JavaScript once the page is refreshed

$('#subregionListPage').bind('pageinit', function(event){ var output = $('#subregions'); var region = getUrlVars()["reg"]; $.ajax({ url: 'http://localhost:8888/my/getsubregions.php', dat ...

Get Facebook to recognize and display link previews for websites using ajax technology

It is commonly known that when copying a link from an HTML website onto Facebook, the platform will attempt to fetch available images as previews for the link. But what happens when the content is dynamically generated, such as with JavaScript? In this c ...

Error 404 occurs when the browser reloads the URL for a specific router route

After uploading my Angular 4 app to a production server, I encountered an issue where the pages work fine when accessed through links but return a 404 error when trying to access them directly via URL. I read about resolving this with .htacces on Apache ...

Unable to dynamically add an element to a nested array in real-time

I'm currently developing an angular tree structure that contains a large nested array. nodes : public fonts: TreeModel = { value: 'Fonts', children: [ { value: 'Serif - All my children and I are STATIC ¯\ ...

What makes (_=1)=>$=>_++ act as a tally?

One of my buddies encountered this question during a JavaScript job interview: Can you explain how this counter functions? In other words, what does the non-minified version look like? let Counter = (_=1)=>$=>_++ let c1 = Counter() co ...

Ending a connection to MS SQL server in node.js with mssql library

In my journey to write a node.js script for querying an MSSQL database, I find myself navigating the realm of JavaScript, node.js, and VSCode with limited SQL knowledge. While I have managed to piece together working code, the issue lies in the connection ...

Display/Conceal a div using CSS in React JS/Redux

I've been working on a CSS toggle for a div with className toggling. My approach involves using redux to manage the boolean state. Using the redux chrome extension, I can see that the state is indeed changing but the div remains invisible. If you have ...

Prevent automatic scrolling of a div in jQuery while it is being hovered over

After addressing a previous question, I have further refined the script but encountered an issue at the final step. The current functionality involves a div automatically scrolling 50px at a time until it reaches the bottom, then it scrolls back to the to ...

Duplicating information within a row

Why am I seeing the same value in all rows of the table? Each row in the table can be clicked to reveal additional information, but every row displays the data from the last clicked row. This issue may be due to saving data in the same object. Unfortunatel ...

Having trouble disabling the Angular-UI Bootstrap accordion?

I am using an angular-ui bootstrap accordion that has a checkbox in the header of each section. The purpose of this checkbox is to determine whether the accordion can be opened or not. Below is the code snippet: <div ng-repeat="b in board | filter:sear ...