Having trouble with data binding in AngularJS?

As a newcomer to AngularJs, I have recently developed a test application using version 1.3.x and encountered a particular issue. Below is the HTML and JS code related to the problem:

<div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var angular.module('ControllerExample', []).controller('SimpleController', SimpleController);

function SimpleController($scope) {
    $scope.customers = [
        { name: 'Customer 1', city: 'Gampaha'},
        { name: 'Customer 2', city: 'Negombo'},
        { name: 'Customer 3', city: 'Gampaha'},
        { name: 'Customer 4', city: 'Gampaha'},
        { name: 'Customer 5', city: 'Kadawatha'}
    ];
}
<!DOCTYPE html>
<html lang="en" ng-app="ControllerExample">

<head>
  <meta charset="utf-8">
  <title>AngularJS App</title>
  <link rel="stylesheet" href="css/style.css" />

</head>

<body>
  <div ng-controller="SimpleController">
    Customer Name:
    <input type="text" ng-model="name" />

    <h3>Adding a Simple Controller</h3>
    <ul>
      <li ng-repeat="cust in customers">{{ cust.name | uppercase }} - {{ cust.city }}</li>
    </ul>
  </div>

  <script type="text/javascript" src="js/script.js"></script>
  <script type="text/javascript" src="js/angular.min.js"></script>
</body>

</html>

Answer №1

An error in syntax has occurred. To resolve this issue, simply delete the var:

The correct code should be: angular.module('ControllerExample', []).controller('SimpleController', SimpleController);

Answer №2

Check this code snippet here: http://jsfiddle.net/adiioo7/jLLyzm7p/

const appModule= angular.module('ControllerExample', []).controller('SimpleController', SimpleController);

function SimpleController($scope) {
    $scope.clients = [{
        fullName: 'Client A',
        location: 'Tokyo'
    }, {
        fullName: 'Client B',
        location: 'Osaka'
    }, {
        fullName: 'Client C',
        location: 'Kyoto'
    }, {
        fullName: 'Client D',
        location: 'Hiroshima'
    }, {
        fullName: 'Client E',
        location: 'Nagoya'
    }];
}

Answer №3

If I understand correctly, you are trying to sort your customers based on their names.

All you need to do is implement a name filter using ng-repeat.

For example, include this code in your controller:

 $scope.filterByName = function(customer) {
     if (!$scope.name) { // handle null case
        return true;
    }

    var searchValue = $scope.name,
        customerName = customer.name;

    var regex = new RegExp('' + searchValue, 'i');

    return regex.test(customerName);
}

Then, in your template, utilize the filter like this:

<li ng-repeat="cust in customers | filter:filterByName"> <!-- Display customers that match the filter -->
     ....
</li>

Answer №4

Executing current version without the need for using "var" Visit this link for code demonstration

<body ng-app="ControllerExample">
  <div ng-controller="SimpleController">
    Customer Name:
    <input type="text" ng-model="name" />

    <h3>Implementing a Basic Controller</h3>
    <ul>
      <li ng-repeat="cust in customers">{{ cust.name | uppercase }} - {{ cust.city }}</li>
    </ul>
  </div>

  <script type="text/javascript" src="js/script.js"></script>
  <script type="text/javascript" src="js/angular.min.js"></script>
</body>







angular.module('ControllerExample', []).controller('SimpleController', SimpleController);

function SimpleController($scope) {
    $scope.customers = [
        { name: 'Customer 1', city: 'Gampaha'},
        { name: 'Customer 2', city: 'Negombo'},
        { name: 'Customer 3', city: 'Gampaha'},
        { name: 'Customer 4', city: 'Gampaha'},
        { name: 'Customer 5', city: 'Kadawatha'}
    ];
}

Answer №5

Consider using

cust["name"]  cust["city"]

in place of

cust.name

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 is the best way to access an element's sibling using JQuery within a function?

How can I use JQuery to set an element to match the value of its sibling? In a table with multiple fields, when clicking an Edit button, I want to copy the label value to its adjacent input field: <table style="width:90%"> <tr> <td&g ...

Increase the day count by 1 on every third cycle

I've been working on a code to increment the days in a date variable every third iteration using modulus. While I have managed to get the logic for every third iteration right, the day doesn't seem to increase by 1. I searched online and found s ...

"Using jQuery to initiate a click action on an anchor element with a different

I'm pondering more of a theoretical question here. Does anyone have any insights on whether or how one can activate the click event on an anchor by clicking on another element using jQuery? My initial solution would involve extracting the src conten ...

Implementing additional input with Jquery causes tooltips to malfunction

I am developing a form that allows users to add as many rows as needed. Each input is being treated as an array for easy data storage in the database, which is a new concept for me. $(document).ready(function() { var maxField = 10; //Limitati ...

Tips for converting JSON or an object into an array for iteration:

I have a JavaScript application that makes an API call and receives JSON data in return. From this JSON data, I need to select a specific object and iterate through it. The flow of my code is as follows: Service call -> GetResults Loop through Results ...

Unable to establish a remote connection to the dlib webserver, but local host connection is functioning

Currently, I am in the process of building a front end using HTML and JavaScript with a C++ backend to handle all calculations. The two are connected through an integrated dlib web server that manages requests. When the frontend requests data, it looks lik ...

Searching for a nested object in Javascript using regular expressions

Presenting my data structure: var foo = { "Category1": [ {"Company1": {"URL": ["DomainName1", "DomainName2"]}}, ... ], ... } Typically, I access DomainName1 like this: foo["Category1"][0][" ...

Refresh the controller variables displayed in the view

I'm currently working on a rails application and looking to refresh/reload the index method of my controller without refreshing the view. For example, here is my index method: def index @title = params[:title] end And the corresponding html.e ...

Only include unique objects in the array based on a common property

I am currently working with the following array: [ {name: "Mike", code: "ABC123"}, {name: "Sarah", code: "DEF456"}, {name: "John", code: "GHI789"}, {name: "Jane", code: "JKL01 ...

Preventing attribute or tag cloning in Jquery: What you need to know

I'm having some trouble with JQuery. I want to clone an attribute or tag that is dragged from one div1 to another div2, which is working fine. But the issue arises when I drag or move the position of an existing tag on div2, then the cloning process s ...

The Dialog feature offered by jQuery-UI

Having just started with jQuery, I am looking to implement the jQuery-UI Dialog to display a message containing lengthy text to the user. My goal is to have a "More details" link in each row of my HTML table that will trigger the jQuery Dialog window to op ...

Is your website's Google analytics event tracking not giving you the feedback you need

Here's what I'm trying to achieve in the code below: Set up Google Analytics on the page Add a jQuery click event based on specific query strings and domain characters Trigger a Google Analytics tracking event with the click event Implement cod ...

How many parameters are typically transmitted through URLs in a nodeJS environment?

Is there a way to identify the number of parameters included in a URL? What approach can I use to count the parameters sent through a URL in nodeJS? ...

The mouse coordinates do not align with the drawing of a rectangle on the canvas

I am having some issues with drawing a rectangle on mouse drag over the canvas that is overlayed on an HTML5 video JS player. The rectangle is being drawn, but it does not align correctly with the mouse coordinates. I suspect that the canvas, which is ove ...

Using JavaScript and AJAX to Transmit an HTML Table to PHP

I am seeking advice on how to send an HTML table to a PHP script using AJAX. The table consists of 10 rows and three columns. While the table is not excessively large, it may be too much data for a single AJAX call. These are my queries: Should I transm ...

Using JavaScript to Sort and Filter HTML <optgroup> Elements

I am encountering an issue with an HTML <select> element that contains <optgroup> elements with <option> elements nested within them. Here is an example of the structure: <select> <optgroup label="label1">Label 1 <op ...

Unable to retrieve Object property using computed method in Vue 3 vuex results in undefined value

When attempting to display the values of a computed property in the template using the syntax {{todo[0]['title']}}, I am receiving an 'undefined' output. However, using {{todo[0]]}} allows me to render the entire object. My goal is to b ...

The function to disable an input field is malfunctioning

Struggling to disable an input field using the disable attribute, I found that changing the element id allowed me to add the disabled attribute through the console successfully. Is there a way to resolve this issue without needing to alter the id? ...

Disabling Android Back Button with JavaScript

Looking for assistance with my HTML app that includes CSS and Javascript. I successfully implemented a feature using JavaScript to prevent users from pressing the back button, which is working well. However, it doesn't seem to work on Android devices ...

Issues arise when trying to access JSON data that has been added to an array using the JavaScript .push method

I am encountering an issue where I cannot retrieve values from an array after storing it in a variable outside of the getJSON function. The response in JSON format looks like this: [ { "Book_ID": "1", "Book_Name": "Computer Architectu ...