What could be causing my AngularJS to malfunction on this particular website?

I am currently learning AngularJs and practicing some coding. However, I am facing an issue where the javascript code does not work when I run it on my browser (Chrome/IE). The "{{product.like}}" code is not functioning as expected. Is there any specific dependencies I need to install or something else that I am missing? Any help or guidance is greatly appreciated. Thank you in advance. Apologies for any language mistakes. :)

HTML CODE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/style.css">
    <title>InstantGram | Posts</title>
    <link rel="icon" type="image/gif/png" href="icon.png">
</head>
<header>
    <h1 class="mainHead"><span id="insColor">Instant</span>Gram</h1>
</header>
<body ng-app="myApp">
    <div class="main" ng-controller="mainController">
        <div>
    <h2 class="sunset">The Unusual Sunset</h2>
    <img src="The_sunset.jpg" alt="Unloaded Image" width="477px" height="268px"/>
    <div class="rating">
            <p class="likes" ng-click="plusOne($index)">+ {{ product.likes }}</p>
        </div>
        </div>
        <hr>
        <div>
    <h2>The Metropolis Before 1927</h2>
    <img src="metropolis_before.jpg" alt="Unloaded Image"/>
    <div class="rating">
            <p class="likes" ng-click="plusOne($index)">+ {{ product.likes }}</p>
        </div>
        </div>
        <hr>
        <div>
    <h2>The Paradise</h2>
    <img src="lake.jpg" alt="unloaded image" width="477px" height="268px">
    <div class="rating">
            <p class="likes" ng-click="plusOne($index)">+ {{ product.likes }}</p>
        </div>
        </div>
        <hr>
        <div class="footer">
            <div class="container">
              <h2>Available for iPhone and Android.</h2>
              <img src="http://s3.amazonaws.com/codecade-content/projects/shutterbugg/app-store.png" width="120px" />
              <img src="http://s3.amazonaws.com/codecademy-content/projects/shutterbugg/google-play.png" width="110px" />
            </div>
          </div>    
          <!-- Modules -->
    <script src="js/app.js"></script>

        <!-- Controllers -->
        <script src="js/controllers/mainController.js"></script>

</body>
</html>

ANGULARJS/JAVASCRIPT CODES:

- App.js

var app = angular.module("myApp", []);

-mainController.js

  app.controller('mainController', ['$scope', function($scope) { 
    $scope.like = 0;
    $scope.plusOne = function(index) { 
      $scope.like[index].likes += 1; 
    };
  }]);

Answer №1

It appears that AngularJS is missing from your application. While you mentioned installing it, it is crucial to also reference it in your code. Below is your original code with AngularJS included at the top, running in a stack snippet for demonstration purposes:

(function() {
  "use strict";
  window.app = angular.module("myApp", []);
}());

(function() {
  "use strict";
  
  app.controller('mainController', ['$scope', function($scope) {
    $scope.like = 0;
    $scope.plusOne = function(index) {
      $scope.like[index].likes += 1;
    };
  }]);
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script>

<body ng-app="myApp">
  <header>
    <h1 class="mainHead"><span id="insColor">Instant</span>Gram</h1>
  </header>

  <div class="main" ng-controller="mainController">
    <div>
      <h2 class="sunset">The Unusual Sunset</h2>
      <img src="The_sunset.jpg" alt="Unloaded Image" width="477px" height="268px" />
      <div class="rating">
        <p class="likes" ng-click="plusOne($index)">+ {{ product.likes }} </p>
      </div>
    </div>
    <hr>
    <div>
      <h2>The Metropolis Before 1927</h2>
      <img src="metropolis_before.jpg" alt="Unloaded Image" />
      <div class="rating">
        <p class="likes" ng-click="plusOne($index)">+ {{ product.likes }} </p>
      </div>
    </div>
    <hr>
    <div>
      <h2>The Paradise</h2>
      <img src="lake.jpg" alt="unloaded image" width="477px" height="268px">
      <div class="rating">
        <p class="likes" ng-click="plusOne($index)">+ {{ product.likes }} </p>
      </div>
    </div>
    <hr>
    <div class="footer">
      <div class="container">
        <h2>Available for iPhone and Android.</h2>
        <img src="http://s3.amazonaws.com/codecade-content/projects/shutterbugg/app-store.png" width="120px" />
        <img src="http://s3.amazonaws.com/codecademy-content/projects/shutterbugg/google-play.png" width="110px" />
      </div>
    </div>

    <!-- Modules -->
    <script src="js/app.js"></script>

Please note that I combined your script modules and controller scripts for the purpose of demonstration above. The key takeaway is to always ensure that you load AngularJS, or any other script you rely on, before utilizing it in your application.

Answer №2

Consider including the following code snippet within your HTML code.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

source: https://docs.angularjs.org/misc/downloading

Answer №3

To get started, choose a version and download the JavaScript file from https://angularjs.org. Then, make sure to add it beneath the head tag in your HTML file. The main issue to be aware of is that the script must be placed within the head tag in order to function properly.

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel = "stylesheet" href = "css/style.css">
<title>InstantGram | Posts</title>
<link rel="icon" type="image/gif/png" href="icon.png">
<script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7a757c6e777a693571685b2a35d50bf">[email protected]</a>" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
<script src="js/app.js"></script>
<script src="js/controllers/mainController.js"></script>
</head>

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

Discover the power of Vue.js 3 by seamlessly integrating anonymous functions within event handlers

Is it possible to invoke an anonymous function within an event handler in Vue.js 3? I came across several options on various websites: <button @click="return function() { console.log('test')}()">Click me</button> <butto ...

Click on the button to automatically scroll to a specific component inside the dialog

In my JSF/Primefaces application, I have a page with a long content that can be scrolled, along with a dialog also containing lengthy content that requires scrolling. The dialog includes a button and I am looking to scroll the dialog content to a specific ...

Guide on using PHP to assign the value of an array to an input field

Here is an array I have: $texts = [ "maybank2u.com", "Open BillPayment", "Status: Successful", "Reference number 2950211545", "Transaction date: 01 Feb 2016 13:09:17", "Amount: RM100.00", ...

How to make Firefox create a new line in a contenteditable field

I am working with a contenteditable div that is within a parent div with a floating width of 50%. My goal is to set the max width of the content editable to match the container, even if the text spans multiple lines. Here is the CSS I tried: .editable-con ...

Position on the chart in the Highcharts

I have developed 2 highcharts in the code below: <div> <p id="container"> </p> <p id="cont"> </p> </div> I specified the width and height as follows: $('#cont').highcharts({ char ...

Searching for client using mqtt.js in Angular2 with Typescript yields no results

I am facing a unique issue while trying to incorporate the mqtt.js library into Angular 2 using TypeScript. Below is my app.component.ts file: import { Component } from '@angular/core'; import * as mqtt from 'mqtt'; @Component({ sel ...

Storing data efficiently with Angular 2's local storage service

I am attempting to create a ToDoList using localstorage within a service. add.component.ts export class AddComponent implements OnInit { item: Item[]; constructor( private router: Router, private itemService: ItemService) { } ...

Several SVG Components failing to function properly or displaying differently than anticipated

I've encountered a puzzling issue that I just can't seem to solve. Here's the scenario: I'm working on a NextJS App and have 5 different SVGs. To utilize them, I created individual Icon components for each: import React from 'reac ...

"My PHP headers are not working properly when I try to

When I invoke a script with JavaScript var user = document.getElementById('username').value; var pass = document.getElementById('password').value; var conf = document.getElementById('confirm').value; var code ...

Vue.js Interval Functionality Malfunctioning

I'm brand new to Vuejs and I'm attempting to set an interval for a function, but unfortunately it's not working as expected. Instead, I am encountering the following error: Uncaught TypeError: Cannot read property 'unshift' of u ...

++first it must decrease before it increases

I'm attempting to create a basic counter feature, where clicking on a button labelled "+" should increase a variable named Score by 1, and the "-" button should decrease it by 1. However, I've encountered an issue where pressing the "+" button fo ...

Use flexbox to manage the width of containers and control the wrapping of elements

Hello, I am a newcomer to utilizing flexbox in CSS. I have these "boxes" that need to maintain their width and wrap when the screen size is small. On a large screen: https://i.sstatic.net/dXnGC.png On a small screen: https://i.sstatic.net/8rZso.pn ...

What is preventing me from choosing the complete table?

I am currently working on a method to export tabular data from an HTML page to Excel using DocRaptor. My approach involves using PHP to pass the necessary data through their API for conversion into an Excel Spreadsheet. However, I have encountered an issu ...

Display the loading status while fetching data

I recently implemented a code snippet that allows users to download files to cache for offline viewing. You can check it out here: While the code works well, I've noticed that for larger files, the loading process can be a bit slow. I am looking for ...

The GET request for a "places" endpoint in a node js application is experiencing issues

Today marks my second day working with node js and mongo. I've explored different tutorials and currently, I'm attempting to follow this specific one: https://github.com/amejiarosario/todoAPIjs/commit/d3be6a287e8aff39ab862971da4f050d04e552a ...

Exclude basic authentication for a specific route

Using node, express and connect for a simple app with basic HTTP auth implemented. The code snippet below shows part of the implementation: var express = require('express'), connect = require('connect'); app.configure = function(){ ...

Finding the difference or sum within an array to identify the two numbers that produce a new array

In order to clarify, I am looking for a method to identify the two smallest numbers in a sorted array that will result in a specific number when subtracted. The process can be broken down into the following steps: Iterate through the array and designate ...

Function wrapper intended for axios

How can I create a wrapper function for axios? I am looking to create a function that can return axios, allowing for easy substitution with another fetch API in the future. This is what I have attempted so far: import axios from 'axios' expor ...

Switch up a JSON string using JavaScript

I received a JS string through an AJAX API call containing data like this: {"Task":"Hours per Day","Slep":22,"Work":25,"Watch TV":15,"Commute":4,"Eat":7,"Bathroom":17} My goal ...

The total height of an HTML element, which takes into account the margin of the element itself

Is there a way to accurately calculate the height of an element including margins, even when dealing with child elements that have larger margins than their parents? HTMLElement.offsetHeight provides the height excluding margin, while this function from ...