Javascript范围在角度SPA应用程序中不起作用
Javascript scope does not work in angular SPA app
所以我有这个角度的SPA,index.html
包含
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>
所以我在已经包含javascript的index.html
中注入了html和javascript。
登录文件类似于
<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>
我的问题是,在login.html
文件中,它不会像应有的方式提醒alert("hey, sup?");
,也不会提醒用户名。
我想 javascript 范围有问题,因为index.html
中有一个,login.html
中有一个。
在login.html
里面,如果我做一个简单的
<button onclick="hey()"> Click </button>
function hey(){
alert("hey");
}
我得到Uncaught ReferenceError: hey is not defined
.
那么这里的javascript有什么问题,我该如何解决这个问题?
谢谢
既然你使用的是角度,那么使用角度的东西呢?
app.controller('loginController', function ($scope) {
$scope.message = 'Login';
$scope.loginData = {};
$scope.hey = function(){
alert('hey '+$scope.loginData.username);
}
});
<input type="text" id="username" name="username" ng-model = "login.Data.username">
<input type="password" id="password" name="password" ng-model="loginData.password">
<button type="submit" id="login" ng-click="hey()"> Login </button>
使用角度,您可以使用表单验证,基本的东西真的很容易在网上找到。如果您需要向服务者提出请求,谷歌$http基础知识。
相关文章:
- 回发前所需的字段验证程序不起作用
- Angular.js ajax和应用程序不起作用
- Rails4-启动多个点击事件处理程序不起作用
- touchstart处理程序之后的touchmove处理程序不起作用
- 我的angular应用程序不起作用
- 按钮在 Chrome 应用中不起作用
- 油脂猴按钮点击处理程序不起作用
- 插入内容后,Iframe 中的单击处理程序不起作用
- 内联点击处理程序不起作用
- 脸书应用程序不起作用
- 委托的事件处理程序不起作用
- 使用 fdb 调试 Adobe AIR JavaScript 应用程序不起作用
- 主干.js事件处理程序不起作用
- 为什么我的 PeerJS 音频聊天应用程序不起作用
- 单击不同 js 文件中定义的事件处理程序和就绪处理程序 - 不起作用
- 邀请朋友尝试脸书应用程序不起作用
- 如果不在 onLoad 函数中,jQuery 事件处理程序不起作用
- 为什么当服务注入第二个控制器时,AngularJS 1.4.8 应用程序不起作用
- 使用gulp-babel ignore编译nodejs应用程序不起作用
- 使用Knockout.js的简单Flickr应用程序不起作用(JSBin准备的)