Navigating Angular: Discovering Route Challenges in Less Than an Hour

Can someone take a look at my code and help me out? I'm trying to learn Angular.js by following the popular Angular.js in 60 minutes video tutorial, but it seems like things have been updated since then. I'm having trouble getting my routes to work properly. Any help would be greatly appreciated!

I'm struggling to figure out what's missing in order to get these routes working correctly.

Here are the scripts from my HTML page:

var demoApp = angular.module('demoApp', ['helperModule']);
    var controllers = {};

    demoApp.controller("CustomersController",  function ($scope){
        $scope.customers = [
                {name: 'Dave Jones', city: 'Phoenix'},
                {name: 'Dave Jones', city: 'New York'},
                {name: 'Jones Suman', city: 'Phoenix'},
                {name: 'Naresh Babu', city: 'Hyderabad'}
            ];
        });
    var demoApp = angular.module('demoApp', ['ngRoute']);
        demoApp.config(function($routeProvider){
            $routeProvider
                .when('/',
                    {
                        controller: "SimpleController",
                        templateUrl: "views/view1.html"
                    })
                .when('partial2',
                    {
                        controller: "SimpleController",
                        templateUrl: "views/view2.html"
                    })
                .otherwise({ redirectTo: "/"})
        });

        $scope.addCustomer = function(){
            $scope.customers.push({name: $scope.newCustomer.name, city: $scope.newCustomer.city});
        }
<script src = "angular-route.js"></script>

These are the views I have for view #1 and view#2. Everything looks correct to me, but I can't seem to get any customer names to appear.

<div cass = "container">
<h2>View 1</h2>
    Name: 
    <input type = "text" data-ng-model="filter.name" />
    <ul><li data-ng-repeat="cust in customers | filter:filter.name"></ul>
    Customer Name: <br />
    <input type= "text" data-ng-model="newCustomer.name" />
    Customer City: <br />
    <input type= "text" data-ng-model="newCustomer.city" />
    <button data-ng-click="addCustomer()"> Add Customer </button>   
    <a href="#/view2"> View 2</a>   
</div>



<div cass = "container">
<h2>View </h2>
<div ng-controller="CustomersController">
    Search: <input type = "text" ng-model="searchText" />
    {{ searchText }}
    <br />
    <h3> Customers </h3>
    <table>
        <tr ng-repeat="cust in customers | filter:city">
                <td>{{ cust.name }}</td>
                <td>{{ cust.city }}</td>
                <td>{{ cust.total | currency}} </td>
            </tr>
    </table>
</div>

Answer №1

It seems like there is an issue with your app override causing problems.

To resolve this, consider starting with

var app = angular.module('app', ['helperModule']);
, ensuring that you declare the app only once.

A better approach would be to define your app with all dependencies initially and then add components such as controllers, directives, factories, etc. as needed.

Here's a code snippet:

// You can remove 'helperModule' if not used
var app = angular.module('app', ['helperModule', 'ngRoute']);
app.config(function($routeProvider) {
    $routeProvider
        .when('/', {
            controller: "SimpleController",
            templateUrl: "views/view1.html"
        })
        .when('partial2', {
            controller: "SimpleController",
            templateUrl: "views/view2.html"
        })
        .otherwise({
            redirectTo: "/"
        });
});

app.controller("CustomersController", function($scope) {
    $scope.customers = [{
        name: 'Dave Jones',
        city: 'Pheonix'
    }, {
        name: 'Dave Jones',
        city: 'New York'
    }, {
        name: 'Jones Suman',
        city: 'Pheonix'
    }, {
        name: 'Naresh Babu',
        city: 'Hyderabad'
    }];

    $scope.addCustomer = function() {
        $scope.customers.push({
            name: $scope.newCustomer.name,
            city: $scope.newCustomer.city
        });
    };
});

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 for clearing Nightwatch session storage efficiently

To ensure the pop-up functionality was working properly, I had to reset the browser's session storage. By doing this, the pop-up will reappear. Is there a way to clear the page's Session Storage in Nightwatch? ...

How can I modify the parent form element to only evaluate the expression at the text node, without affecting Angular interpolation?

Currently, I am in the process of developing an eCommerce platform and utilizing angular to construct a widget on product detail pages. Unfortunately, my control over the initial HTML rendered for the browser is quite limited. While most tasks related to m ...

Customize.Print - establish default print preferences

In my current project, I am exploring ways to add a print PDF feature. After discovering print.js, it seems like a possible solution for this task. My main concern is setting default print options to ensure the correct scale (i.e., 100%). Does print.js h ...

Implementing Pagination or Infinite Scroll to Instagram Feed

Currently, I am working on creating an Instagram feed for a fashion campaign where users can hashtag their photos with a specific tag. Using the Instagram API, the script will pull all recent posts with this common tag to display on the webpage. Instagram ...

The error message "Uncaught TypeError: Cannot read property '0' of undefined" is triggered when using toDataURL

Recently diving into the world of JavaScript and facing a perplexing error. I must be overlooking some fundamental concept... apologies in advance. Here is the issue at hand. In my HTML file, this snippet of code is present: <div> <script type= ...

What is the best way to retrieve the ajax response using Ajax.Responders in prototype.js?

I am looking to retrieve the response of each Ajax call within the function below Ajax.Responders.register({ onCreate: function() { }, onComplete: function(transport) { }, onSuccess: function(transport) { }, }); ...

Sending Angular Material Select Option Value for Submission

HTML: <form id="reg" name="reg" enctype="application/x-www-form-urlencoded" action="http://api.phphotspot.com/v-2/client-register" method="post"> <md-input-container class="md-block"> <label for="country">Country</label&g ...

Transitioning from the traditional LeftNav menu style to the more versatile composability feature using Reactjs with Material-ui

Hey there, I'm facing a bit of confusion while trying to create a LeftNav menu using material-ui. I recently joined this project and updated reactjs and material-ui. Unfortunately, many things related to LeftNav in material-ui have been deprecated, ...

What is the reason the child component is not being displayed?

The code within the APP.js component looks like this: import React from "react"; import Exam from "./exam.js"; export default function App() { return ( <Exam> <h1>hashemi</h1> </Exam> ); } Similarly, the ...

Something is seriously wrong with the datetime in fullcalendar JavaScript

I've been diving into a tutorial for creating a calendar scheduler in asp.net MVC5 from this link. One issue I'm facing is the datetime being passed and stored as the min value in the database (1/1/0001 12:00:00 AM), almost like it's null b ...

Unlimited possibilities with parameters on an express server

I've set up React Router with recursive parameters, similar to the example here. On my Express server, I'm attempting to handle routes like /someRoute/:recursiveParameter? router.get('/someRoute/:recursiveParameter?', async (req, res ...

Changing the entire content of a webpage from the server using AJAX

I am looking to update the entire page content with the click of a button, transitioning from Words.html to SelectNumber.html This snippet is from Words.html <html> <head> <meta charset="UTF-8"> <title>Number Game< ...

What steps should I take to fix the error message "Uncaught TypeError: Class constructor m must be called with 'new'" that occurs while attempting to access a polymer v2.0 application?

Is there a way to resolve this error that occurs when attempting to open a Polymer v2.0 app on Safari in iOS? Uncaught TypeError: Class constructor m cannot be invoked without 'new' from custom-elements-es5-adaptor. The Polymer v2.0 starter k ...

The findByIdAndUpdate() function lacks the ability to modify the collection

I'm encountering an issue when trying to update a product using mongodb and redux. It seems that the database is not reflecting the changes after I attempt to update the product. Can someone please assist me with this problem? Here is my product.js f ...

Using D3.js for Page Navigation

For my D3 bar charts, I am working with a substantial amount of JSON data obtained from an API. My goal is to display only 10-20 bars at a time. Would it be possible to implement pagination using D3 itself, or should I explore alternative methods (such a ...

JavaScript if statement can be used to evaluate two variables that have the same values

I am currently working on a Wordle-style game with a 6x6 grid. I'm sending the row as an array through a socket, and while I can check if a letter is correct, I'm having trouble with identifying duplicates that are in the wrong position. I iterat ...

What sets Redux React-Native apart: Exploring the nuances between utilizing useSelector in react-redux versus connect

I believe they were identical because both extracted the content from the store. What could potentially differentiate them? ...

Is there a way to display the "back button" on a subview?

As a newcomer to Ionic, I am finding navigation to be the most challenging aspect. My app has two tabs - "Dashboard" and "Friends". When I click on the Dashboard tab, I want it to navigate to a subview called "subview_dash", without displaying the tabs in ...

Learn how to render list items individually in Vue.js using the 'track-by $index' directive

Recently, I switched from using v-show to display elements in an array one at a time in my Vue instance. In my HTML, I had this line: <li v-for="tweet in tweets" v-show="showing == $index">{{{ tweet }}}</li>". The root Vue instance was set up l ...

Using the concept of method chaining in JavaScript, you can easily add multiple methods from

Hey there! I'm looking for some assistance with dynamically building a method chain. It seems like it should be pretty straightforward if you're familiar with how to do it... Currently, I am using mongoose and node.js to query a mongo database. ...