Home > Software design >  AngularJS --> ng-view not loading js
AngularJS --> ng-view not loading js

Time:12-28

The code works when the day1.js script is loaded in the index and if a certain snippet from the HTML template is pasted into the file, but it doesn't work when I switch to the page using ng-view with the same code. There is nothing in my day1controller. I'm pretty lost at this point and would appreciate some insight.

I keep getting the error

day1.js:5 Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

Here is my index:

<!-- Takes in an input.txt and counts how many times the numbers increase and decrease -->
<!DOCTYPE html>
<html ng-app="adventOfCode">
  <head>
    <title>Home</title>
    <link rel="stylesheet" href="style.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular-route.min.js"></script>
    <meta name="vieport" content="width=device-width initial scale=1" />
  </head>
  <body>
    <header><h2>Website Header</h2></header>
    <div >
      <div >
        <a href="#/home">Home</a><br />
        <a href="#/day1">Day 1</a><br />
        <a href="#/day2">Day 2</a><br />
      </div>
    </div>

    <!-- Where the pages change -->
    <div ng-view></div>

    <!-- Code only works as intended when this div is pasted here -->
    <div>
      <input type="file" />
      <textarea name="" id="" cols="30" rows="10"></textarea>
    </div>
    <!-- End of pasted code -->

    <footer><h3>Website footer lalalala</h3></footer>

    <script>
      var app = angular
        .module("adventOfCode", ["ngRoute"])
        .config(function ($routeProvider, $locationProvider) {
          //inject $locationProvider service
          $locationProvider.hashPrefix(""); // add configuration
          $routeProvider
            .when("/home", {
              template: "About Us",
            })
            .when("/day1", {
              templateUrl: "views/day1.html",
              controller: "day1Controller",
            })
            .when("/day2", {
              templateUrl: "views/day2.html",
              controller: "day2Controller",
            });
        });
    </script>

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

    <!-- My scripts -->
    <script src="js/day1.js"></script>
  </body>
</html>

` Here is the javascript I am trying to run (day1.js):

//selecting the input and textarea elements and saving them to variables
let input = document.querySelector("input");
let textarea = document.querySelector("textarea");

input.addEventListener("change", () => {
  //returns an array of File objects
  let files = input.files;
  if (files.length == 0) return;

  //getting the first File object
  const file = files[0];

  //creating a FileReader
  let reader = new FileReader();
  reader.onload = (e) => {
    var index = 0;
    var lastLine = "";
    var currentLine = "";
    var increased = 0;
    var decreased = 0;
    var results = [];

    const file = e.target.result;
    console.log("inside onl oad");
    /**We are using split() function and passing regex pattern /\r\n|\n/ as a parameter. This will generate an array of lines and we are storing that in the lines variable. */
    const lines = file.split(/\r\n|\n/);
    console.log("split the lines");
    /**-------------- Our Workspace -------------- */
    lines.forEach((line) => {
      if (index === 0) {
        lastLine = line;
        console.log(line   "-->"   index);
        index  ;
      } else {
        currentLine = line;

        if (currentLine > lastLine) {
          console.log(line   " --> "   index   "  :increased");
          increased  ;
        } else if (currentLine < lastLine) {
          console.log(line   " --> "   index   "  :decreased");
          decreased  ;
        } else {
          console.log(line   " --> "   index);
        }
        index  ;
        lastLine = currentLine;
      }
    });

    console.log("Number of inputs: "   index);
    console.log("How many times the inputs increased: "   increased); //1582 is too low
    console.log("How many times the inputs decreased: "   decreased);

    document.getElementById("increase").innerHTML =
      "How many times the inputs increased: "   increased;
    document.getElementById("decrease").innerHTML =
      "How many times the inputs decreased: "   decreased;

    results = slidingWindow(lines, 3);

    document.getElementById("increase_").innerHTML =
      "How many times the inputs increased: "   results[0];
    document.getElementById("decrease_").innerHTML =
      "How many times the inputs decreased: "   results[1];

    document.getElementById("Index").innerHTML = "Number of inputs: "   index;

    /**We are using the join() method to join all lines by a newline character (\n). This will return a string and we are setting that string as the value of the textarea element. */
    textarea.value = lines.join("\n");
    console.log("joined the lines");
  };
  reader.onerror = (e) => alert(e.target.error.name);
  reader.readAsText(file);
});

function slidingWindow(linesArray, windowSize) {
  if (windowSize < 0 || windowSize > linesArray.length) return null;
  let currentSum = 0;
  let lastSum = 0;
  let increased = 0;
  let decreased = 0;
  let results = [];
  for (let i = 0; i < linesArray.length; i  ) {
    currentSum  = parseInt(linesArray[i]);
    if (i >= windowSize - 1) {
      if ((lastSum === 0) && (currentSum > lastSum)) {
        console.log(currentSum   " --> "   i   "  :no change");
      } else if (currentSum > lastSum) {
        console.log(currentSum   " --> "   i   "  :increased");
        increased  ;
      } else if (currentSum < lastSum) {
        console.log(currentSum   " --> "   i   "  :decreased");
        decreased  ;
      } else if ((currentSum = lastSum)) {
        console.log(currentSum   " --> "   i   "  :no change");
      }
      lastSum = currentSum;
      currentSum -= linesArray[i - (windowSize - 1)];
    }
  }
  return (results = [increased, decreased]);
}

Here is day1.html, which is what I'm want to use to run day1.js:

<div>
    <input type="file">
    <textarea name="" id="" cols="30" rows="10"></textarea>
</div>

      <div>
          <h3>Increase or Decrease</h3>
          <h2 id="increase"></h2>
          <h2 id="decrease"></h2>
          
          <h3>Sliding window</h3>
          <h2 id="increase_"></h2>
          <h2 id="decrease_"></h2>
          <h3 id="Index"></h3>
      </div>

CodePudding user response:

You have to use container for place your view.ng- view is a directive that works like a placeholder. It creates a placeholder where a corresponding view can be placed based on the configuration.

<div >
  <div >
    <div >
      <div >
        <a href="#home" >Home</a>
        <a href="#about" >About</a>
      </div>
    </div>
  </div>
</div>

<div >
  <div ng-view=""></div>
</div>

CodePudding user response:

My problem was that I didn't have the Jquery library in my index.html. Without it the scripts aren't executed when loaded by ng-view.

  • Related