So I have this angular SPA, the index.html
contains
var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider , $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.html',
controller: 'homeController'
})
.when('/login', {
templateUrl: 'login.html',
controller: 'loginController'
})
});
app.controller('homeController', function ($scope) {
$scope.message = 'Welcome';
});
app.controller('loginController', function ($scope) {
$scope.message = 'Login';
});
<div ng-view></div>
So I inject html and javascript inside the index.html
that already contains javascript.
The login file is like
<p>{{ message }}</p>
<form >
<input type="text" id="username" name="username">
<input type="password" id="password" name="password">
<button type="submit" id="login"> Login </button>
</form>
<script type="text/javascript">
document.getElementById("login").onclick = handleButtonPress;
function handleButtonPress(e) {
e.preventDefault();
alert("hey, sup?");
var form = document.getElementById("loginform");
var formData = new FormData(form);
var j = JSON.stringify(formData);
alert(" formData > " + j.username);
}
</script>
My problem is , inside the login.html
file it does not alert the alert("hey, sup?");
as it should and it does not alert the username.
I guess is something wrong with the javascript scope, because there is one in the index.html
and one in the login.html
.
Inside the login.html
if I do a simple
<button onclick="hey()"> Click </button>
function hey(){
alert("hey");
}
I get Uncaught ReferenceError: hey is not defined
.
So what is the problem with javascript here and how can I solve this ?
Thanks