Having trouble with your Angular routes tutorial?

I'm currently working on Google's Angular tutorial log (with different names for the necessary items), and I can't seem to get the routing to function correctly. If I remove the "#" from the hrefs, I end up with a "Cannot GET" error. But even with the "#", the URL changes but nothing happens.

controllers.js

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

AppControllers.controller('homeCtrl', ['$scope', '$http',
    function($scope, $http){
  $http.get('javascripts/apprentices.json').success(function(data){
        $scope.apprentices = data;

  });
  $scope.orderProp = 'semester';
}]);

AppControllers.controller('apprenticeCtrl',['$scope', '$routeParams',
    function($scope, $routeParams) {
console.dir('hi');
      $http.get('javascripts/person.json').success(function(data) {
        // $scope.apprentices = data;
      });
    }]);

app.js

var App = angular.module('App',[
  'ngRoute',
  'AppControllers']);

App.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
    when('/apprentices', {
        templateUrl: 'views/apprentice.html',
        controller: 'apprenticeCtrl'
      }).
      otherwise({
        templateUrl:'../views/home/index.html',
                redirectTo: '/asfsfsf'
      });
  }]);

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="javascripts/angular/angular.js"></script>
    <script src="javascripts/angular/angular-route.js"></script>
    <script src="javascripts/app.js"></script>
    <script src="javascripts/controllers.js"></script>
    <body>
      <div ng-app="App">
        <div ng-controller="homeCtrl">
          code
      </div>
      <div ng-view></div>
    </body>
  </head>
</html>

Answer №1

To ensure the proper scope of your app, consider moving the ng-app tag up to the body element. This way, the app's scope will be defined within the ng-app element and not among its siblings:

<!DOCTYPE html>
<html lang="en>
  <head>
    <title></title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="javascript/angular/angular.js"></script>
    <script src="javascript/angular/angular-route.js"></script>
    <script src="javascript/app.js"></script>
    <script src="javascript/controllers.js"></script>
    <body ng-app="App">
      <div>
        <div ng-controller="homeCtrl">
          code
      </div>
      <div ng-view></div>
    </body>
  </head>
</html>

Alternatively, you could place the ng-view div inside the ng-app div if desired.

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

The Vue component is only functioning partially and is not visible in the Vue inspector

I have a straightforward Vue component that is causing me some trouble. <template> <div class="field has-addons"> <div class="control is-expanded"> <div class="select is-fullwidth"> <select v-mode ...

Guide on accessing Azure Configuration Application settings in a Node application with JavaScript

Currently, I have a node.js application named "myClient" deployed on Azure as an App Service. Within this setup, I utilize several configuration files that contain specific values tailored to their respective runtime environments: appconfig.json is used f ...

Trigger the function upon successful completion of Stripe Checkout

I have a requirement in my Nodejs/Express API to execute a series of confirmation code once the checkout process is successfully completed in Stripe by the client. Below is the checkout function responsible for handling the checkout phase: // Checkout con ...

angularjs routing based on conditions

Is there a way to pass a hidden value or optional parameter that is not part of the URL in order to switch between states based on that parameter? I attempted something like this: <a ui-sref="state2({key:'value', optional: 'B'})"> ...

After successfully executing an AJAX request three times, it encountered a failure

I have implemented a script to send instant messages to my database asynchronously. Here is the code: function sendMessage(content, thread_id, ghost_id) { var url = "ajax_submit_message.php"; var data = { content: content, thread_id: thread_id }; ...

angular ng-model behaving strangely

Sample Code: <select id="siteOptions" ng-model="siteVal" ng-change="siteChange()"> <option ng-repeat = "site in sites" ng-selected="{{site == selectedSite}}" value="site">{{site}}</option> </select> Function Definition: $ ...

Is there a way to refresh the list automatically after deleting an item using Firebase?

When I use ngFor on a list to display multiple recordings in a table, I have two methods in my TypeScript file - one for getAll and another for delete, as shown below: HTML: <tr *ngFor="let value of imagesList"> <td scope="row& ...

Retrieve the chosen language while utilizing the $translate feature

I have successfully integrated the $translate project from this source into my AngularJS application. While I have configured the default language in my app.config() using $translateProvider, I am now wondering how to retrieve the selected language within ...

Is it possible to utilize a locally installed module with npm and incorporate it into a requirement statement?

When attempting to install a local package, the following command can be used: npm install </path_of_package> If wanting to use it in a require statement, how should it be done? For instance, if having downloaded the latest alpha version of puppete ...

Produce HTML content onto Google Drive Documents using JavaScript

Currently, I am working on a project that requires me to render the HTML form output in a new Google Docs document (a Word file, not a spreadsheet). Despite my efforts to find information online, all I can come across is related to spreadsheets. The main ...

Angularjs $state.go is a powerful tool for navigating through different states

I've been working on setting up a basic navigation system for my single-page application. It consists of a series of links in a list, each linked to a method in a NavigationController. The method then triggers $state.go by passing it a state string. I ...

javascript game for reversing an array

in case(po==true){ snake_array.reverse(); var i=0; var c=snake_array[i]; //drawing the head draw_head(c.x,c.y); for(i=1;i<snake_array.length;i++){ //drawing the body var c=snake_arr ...

What is the best way to link a variable to the content of my HTML input using jQuery?

When working with a form that uses the post method, there is often a need to encrypt certain parameters using md5 before posting them in JavaScript. How can I bind these encrypted values to the form? Here is an example of an HTML form: <input id="sig" ...

Adding Node Modules during the setup of an ElectronJS application

Hey there! I'm currently working on an ElectronJS application designed for developers. One of the key features is checking for the presence of NodeJS on the user's computer. If it's not detected, the app will automatically download and insta ...

Optimal method for delivering static JavaScript from a node module's "dist" directory with Next.js

Currently, I am working on an application that utilizes Tesseract (OCR) to extract text from images. I am interested in making some JS files from node_modules/tesseract.js/dist downloadable directly in the browser. While I can simply copy the files to ./ ...

Adjust the color of the background when hovering with the cursor

I am currently working on a project with a menu of buttons that dynamically changes based on the elements in a list. For instance, if my list contains ["A", "B", "C"], then I would have a menu with 3 buttons. Here is how I display the buttons using a java ...

Issue with HTML5 Canvas y-axis dimensions

I am attempting to create a basic animated HTML canvas with a moving block controlled by WASD keys. I encountered an issue where drawing a 5x5 rectangle appeared to be 5x10 on the canvas. Upon inspecting my code and logging the position of the element, I d ...

Manipulating JSON Objects in AngularJS

While I may be proficient in utilizing existing data, my expertise in editing JSON objects is not quite up to par yet. Any assistance in this area would be greatly appreciated. This question may seem basic, but I am seeking clarification as I am unsure. H ...

Get information from the server via an ajax callback without refreshing the page and dynamically refresh the current page content. Node.js and Express 4

I am trying to implement a form and an Ajax client script that sends the form data to Express. The code for the form looks like this: form#form-reflow(action='/', method='post', onsubmit="docreflow()") input#te ...

Insert fresh user information into the div

Learning JavaScript is a challenge I'm tackling. I have a question that may seem trivial, but any assistance would be greatly appreciated. I currently have this code: Javascript <script type="text/javascript"> function fn(){ var Name = ...