當 day1.js 腳本加載到索引中并且如果 HTML 模板中的某個片段被粘貼到檔案中時,代碼會起作用,但是當我使用具有相同代碼的 ng-view 切換到頁面時它不起作用。我的 day1controller 中什么都沒有。在這一點上我很迷茫,希望得到一些見解。
我不斷收到錯誤
day1.js:5 未捕獲的型別錯誤:無法讀取 null 的屬性(讀取“addEventListener”)
這是我的索引:
<!-- 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 class="column">
<div class="links">
<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>
` 這是我嘗試運行的 javascript (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]);
}
這是 day1.html,我想用它來運行 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>
uj5u.com熱心網友回復:
你必須使用容器來放置你的 view.ng-view 是一個像占位符一樣作業的指令。它創建了一個占位符,可以根據配置放置相應的視圖。
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="nav">
<a href="#home" class="button">Home</a>
<a href="#about" class="button">About</a>
</div>
</div>
</div>
</div>
<div class="container">
<div ng-view=""></div>
</div>
uj5u.com熱心網友回復:
我的問題是我的 index.html 中沒有 Jquery 庫。沒有它,ng-view 加載時不會執行腳本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399264.html
