Angular.js:10671 Uncaught TypeError: Cannot read property of undefined

I am currently in the process of learning Angular through a course, despite having limited experience with JavaScript. I have been struggling to make ng-class function properly and cannot figure out why I keep encountering the error mentioned in the title. Here is my hotels.js file:

 'use strict'

 //MODULE
 var app = angular.module('hotelsApp', ['ngRoute']);

 app.controller('HotelsController', ['$scope', function($scope){
 $scope.rooms = mockRooms;

 $scope.promoted = function(room) {
  if(room.promotion !== null && room.promotion !== undefined) {
      return true;
  } else {
      return false;
  }
 }

$scope.booking = function(room){
  if(room.beds > 1){
    alert(room.size + " with " + room.beds + " beds, is booked");
  }
  else
    alert(room.size + " with " + room.beds + " bed, is booked");
};

}]);

var mockRooms = [{
  "size": "studio",
  "beds": 2,
  "number": 100,
  "kitchen": true,
  "price": "100$",
  "booked": false, 
  "promotion" : {
      "discount" : "30%",
      "message" : "Reserve it today!",
      "price" : "70$"
  }    
},
{ "size": "queen",
  "beds": 2,
  "number": 101,
  "kitchen": true,
  "price": "35$",
  "booked": true }      
];

And here is my index.html:

 <!DOCTYPE html>
 <html lang="en-us" ng-app="hotelsApp">
 <head>
 <title>Hotels app</title>
 <meta http-equiv="X-UA-Compatible" content="IE=Edge">
 <meta charset="UTF-8">

  <!-- load bootstrap and fontawesome via CDN -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" src="css/main.css" />
<script src="//code.angularjs.org/1.3.0-rc.2/angular.js"></script>
<script src="//code.angularjs.org/1.3.0-rc.2/angular-route.min.js">   </script>
</head>
<body ng-controller="HotelsController">
  <div ng-repeat="room in rooms">
    <h4>{{ room.size }}</h4>
    <p>Beds: {{ room.beds }}</p>
    <p>Room number: {{ room.number }}</p>
    <p ng-show="room.kitchen">
    Kitchen available
    </p>
    <p ng-hide="room.promotion">
    Price: {{ room.price }}
    </p>
    <p ng-class="{heavy:promoted(room)}" ng-hide="!room.promotion">
    Save {{ room.promotion.discount }} {{ room.promotion.message }} Price: {{ room.promotion.price }}
    </p>
    <button ng-hide="room.booked" ng-click="booking(room)">Book now!</button>
 </div>

 <script src="app/hotels/hotels.js"></script>
</body>

Answer №1

There seems to be an issue with your links as they are all broken:

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" src="css/main.css" />
<script src="//code.angularjs.org/1.3.0-rc.2/angular.js"></script>
<script src="//code.angularjs.org/1.3.0-rc.2/angular-route.min.js">   </script>

Please make sure to add "https://" in front of all CDNs like so:

Here are the corrected CDN links for you to use:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.min.css" />    
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-route.min.js"></script>

https://i.sstatic.net/Dgg5g.png

Answer №2

Switching the link attribute in main.css from src to href was a simple fix for my mistake. It's strange that the console was throwing an undefined property error before, which left me puzzled.

<link rel="stylesheet" type="text/css" href="css/main.css" />

Answer №3

Wow, this one really stumped me. If you swap out src for href in the link tag to load your custom css and then refresh the page, it should work like a charm.

You've got everything set up correctly, just missing the css loading.

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 set a specific image as the initial image when loading 'SpriteSpin'?

I'm currently working on creating a 360-degree view using the SpriteSpin API. Everything is functioning as expected, but I have an additional request. I need to specify a specific image to be the initial landing image when the page loads. The landing ...

What is the best way to load data that is essential for the entire functionality of an AngularJs Application?

Within my application, there exists essential data that is utilized by various controllers to carry out their functions. For instance, the user and other entities must make REST calls to the server. Currently, I have an AppController (app.js) responsible ...

Efficiently managing modules with requirejs and Backbone.Marionette

After organizing the file structure of my web app, utilizing RequireJs and Backbone.Marionette, it now looks like this: |- main.js |- app.js |- /subapp1 |- subapp1.js |- subapp1.router.js |- /subapp2 |- subapp2.js | ...

Encountering difficulties in sending the data to Firebase through the contact form

import React, { useState } from 'react' import styled from 'styled-components' import Title from '../Components/Title' import { InnerLayout, MainLayout } from '../Styles/Layout' import ...

Stay dry - Invoke the class method if there is no instance available, otherwise execute the instance method

Situation When the input is identified as a "start", the program will automatically calculate the corresponding "end" value and update the page with it. If the input is an "end", simply display this value on the page without any modifications. I am in th ...

Issues encountered with JSON formatting following jQuery ajax request

When my nodejs app receives data from a cordova app through a jQuery ajax call, the format is different. It looks like this: { "network[msisdn]": "+254738XXXXXX", "network[country]": "ke", "network[roaming]": "false", "network[simSt ...

How can I access a child div in the parent element that I am

<script type="text/javascript"> $(".inventory-notes").bind("click", function() { if($(this).text().length > 0) { alert($(this).text()); alert($(this).closest(".inventory-notes").children().text()); } }); </script> ex ...

Instructions for implementing an inline toolbar in a contenteditable="true" feature

I am currently developing a page that allows users to edit its content directly on the page. To achieve this, I have set the attribute contenteditable="true" for all the elements within the page. My goal is to show an inline toolbar with basic editing op ...

A different approach to including a script tag following each AJAX element

I seem to be encountering an issue, but I'm unsure of what it could be. I have a gallery containing a list of links, each link triggers the loading of various div elements asynchronously using the Jquery load() method. HTML ... <sec ...

Encountering a connection refusal error message while executing Selenium-webdriver

I am attempting to view the raw HTML content before it is displayed by the browser. To achieve this, I am utilizing selenium-webdriver in a node.js environment. Below is my code snippet along with the error message I encountered: Code var webdriver = requ ...

Is it possible to modify CSS properties by clicking on buttons?

Hello, I really need help with this task. I want to change the style of a button (which is actually a link) by clicking on it. These buttons are housed within a dynamic child div that changes each time, but the name of the dynamic child div remains con ...

Pressing a button to alter the text color

While experimenting, I attempted to alter the text color of my HTML using JavaScript. After a few attempts, I came to the realization that I cannot change the color if I am already changing it in CSS. Does this mean CSS is executed at the end? Also, how ca ...

What is the correct way to navigate data through my Node.js API?

I am struggling with getting the JSON response to display at the /api/orders_count endpoint. Here is a breakdown of my setup: In my project, I have various files: Routes file - where the orders_count route is defined: routes/index.js const express = req ...

Testing the revised react component through unit testing with jest and enzyme

I am currently working on writing the test file for this code snippet. Here is my approach: import React from 'react'; import renderer from 'react-test-renderer'; // import { mount } from 'enzyme'; import LazyToastMessage from ...

Encountering a problem with AngularJS when trying to pass a controller through ng-include in HTML

I'm currently developing a portal-style application that will dynamically inject HTML from other URLs (within the domain) into different sections of the page, which I like to refer to as widgets. Each widget needs to be loaded with an ng-include and h ...

What is the best way to display a success notification when a transaction is successfully executed in web SQL

var brand = "Glare"; var price = "3000$"; var color = "blue"; var success = tx.executeSql("INSERT INTO truck (brand, price, color) VALUES ('"+brand+"','"+price+"','"+color+"' ")"); if(success) { alert("successfully insert ...

Exploring AngularJS and Jasmine: Testing a controller function that interacts with a service via $http

I encountered an issue while testing a controller that relies on a service. The problem arises because the service is currently set to null in order to focus solely on testing the controller. The current test setup is failing due to the BoardService being ...

Determining the file path relative to the project/src directory in Node.js

Currently, in my node.js project I am utilizing log4js and aiming to include the file name where the log record was added. However, when using __filename, it provides me with the absolute path. var logger = log4js.getLogger(__filename) This results in lo ...

Is there a way to automatically fill up a second dropdown menu based on the user's selection from the first dropdown

Using jQuery or vanilla JavaScript, I am looking to dynamically populate a second dropdown based on the selection made in the first dropdown. The value chosen in the first dropdown will determine which options are displayed in the second dropdown. What is ...

Toggling classes in Angular for parent elements

Using an Angular Directive to Toggle Class: app.directive('toggleClass', function() { return { restrict: 'A', link: function(scope, element, attrs) { element.bind('click', function() { ...