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

What methods can I leverage in VueJS to redirect users to a different page or URL?

I am new to JavaScript, so please excuse any mistakes in my code. I am working on a form that utilizes AWS SNS to send an email to my company with the data submitted through the form. The issue I'm facing is that there is no feedback for the user afte ...

Harnessing the power of JavaScript functions to display an image when clicked

Looking for help with making an image appear when clicking on one of three images? Despite trying different approaches, the desired result is still not achieved. I'm aware of using if else statements but exploring other methods. Any insights on what m ...

How can I ensure that each callback is passed a distinct UUID?

I am utilizing a package called multer-s3-transform to modify the incoming image before uploading it to my bucket. Below is the code snippet of how I am implementing this: const singleImageUploadJpg = multer({ storage: multerS3({ s3: s3, bucket: ...

Storing and Manipulating a JavaScript Object with Vuex: A New Perspective

Consider a hypothetical JavaScript object class like this: class Car { var engineTurnedOn = false; ... public turnEngineOn() { engineTurnedOn = true } } If I want to turn the engine on, should I create an action called 'turnEngineOn&ap ...

What could be causing my code to become unresponsive when using a for loop compared to a loop with a specific

After spending a solid 4 hours on it, I finally managed to figure it out. There were no errors popping up, so I resorted to using the debug feature which unfortunately didn't provide much insight. Without any error messages to guide me, I wasn't ...

Is the functioning of closures identical when applied to class methods?

Recently, I started learning React.js (along with Javascript) and I have a basic question to ask. I have created a small component that consists of 3 buttons. Each time these buttons are clicked, the value increments by one. Here is a working example: cl ...

Styling components using classes in Material-UI

I recently started using material-ui and noticed that it applies inline styles to each component. After running a test with multiple instances of the same component, I realized that there was no CSS-based styling - only repeated inline styles were generate ...

What is the reason behind Chrome's automatic scrolling to ensure the clicked element is fully contained?

Recently, I have observed that when performing ajax page updates (specifically appends to a block, like in "Show more comments" scenarios) Chrome seems to automatically scroll in order to keep the clicked element in view. What is causing this behavior? Wh ...

The default value is not displayed in the Angular dropdown menu

When using regular html select menus, if you create an option element with selected and disabled attributes and provide text for that option, the text will be displayed by default in the select menu. Below is a basic example of HTML code: <select name= ...

Is there a method in AngularJS to have $http.post send request parameters rather than JSON?

I have come across some older code that utilizes an AJAX POST request using jQuery's post method. The code looks something like this: $.post("/foo/bar", requestData, function(responseData) { //do stuff with response } The request ...

Ways to assign a CSS class specifically for images

.calendarList { background-image: url('/resource3/hpsc/common/images/calendar.png'); background-position: 135px 50%; background-repeat: no-repeat; cursor:pointer; } <input type="text" id="toDatepicker" class="cal ...

The function $(...) does not recognize tablesorter

Currently, I am encountering issues with the tablesorter plugin as my system is unable to recognize the existing function. It is unclear whether there might be a conflict with other JavaScript files, especially since I am implementing changes within a Word ...

Tips for managing large amounts of data retrieval from an API

As a beginner, I am attempting to retrieve data from an API and display it using the v-for directive. However, this process is causing my app to lag. It freezes when new data is fetched or when I search within the list. The following code snippet shows whe ...

Prevent the <a> tag href attribute from functioning with jQuery

I came across this code snippet: <script type="text/javascript"> $(document).ready(function() { $("#thang").load("http://www.yahoo.com"); $(".SmoothLink").click( function() { $("#thang").fadeOut(); $("#thang").load( ...

What could be the reason for the empty array returned by the combinationSum function in Javascript?

The combinationSum function is returning an empty resultArr. When checking the ds array with console.log, it shows the correct answer, but for some reason, the final output array ends up being [[],[]]. var combinationSum = function(candidates, target) { ...

I am encountering an issue where I am sending an AJAX request to a PHP file with the datatype set as JSONP, but I

When I click on the submit button, I am sending a variable to sendmail.php. However, PHP is showing that 'contactname' is undefined. Why is this happening? Here is the code snippet: var name = document.getElementById('stream_cotactname&apo ...

A step-by-step guide on setting up flow types for @material-ui/core version 4

Is there a way to install flow types for material-ui/core version 4.x.x? It seems like the last update was for version 1.x.x here. The documentation on this topic is quite limited here. I'm unsure if there is still support for this, especially since t ...

Combine the remaining bars by stacking the highest one on top in Highchart

Making use of stacking to display the highest value as the longest column/bar, with smaller values being merged within the highest one, can create a more visually appealing stack chart. For example, when looking at Arsenal with values of 14 and 3, ideally ...

Experiencing AJAX errors 403 and 404 despite successful implementation in other instances

I am facing a perplexing issue with my code that is causing a 403 error when attempting to delete a row. The strange thing is, the code works perfectly on another website I created. The concept is quite simple - attaching an event listener to a button trig ...

ESLint has detected a potential race condition where the `user.registered` variable could be reassigned using an outdated value. This issue is flagged by the `require-atomic-updates` rule

I have developed an asynchronous function which looks like this: let saveUser = async function(user){ await Database.saveUser(user); if (!user.active) { user.active = true; //storedUs ...