我对AngularJS和javascript相对较新,请回答时请怜悯。
我正在尝试创建一个我希望嵌套控制器的示例应用程序。 MainController将始终作为父容器,如果用户登录将在顶部呈现菜单和用户名。现在我正在检查用户是否存储在localStorage中。
每个页面都有自己的控制器,可以执行页面特定的操作。
我坚持使用ng-submit以及为什么它不起作用?
感谢任何帮助。
的index.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-resource.js"></script>
<script src="angularapp.js"></script>
</head>
<body ng-app="myAngularApp">
<div ng-controller="MainController">
<div ng-show="isLoggedIn">Menu</div>
<div ng-view>
</div>
</div>
</body>
的login.html
<from ng-submit="login()">
<input type="text" id="username" ng-model="username"></input>
<input type="password" id="password" ng-model="password"></input>
<input type="submit" value="submit" id="submit" ng-submit="login()"></input>
</form>
angularapp.js
angular.module('myAngularApp',['ngRoute','ngResource'])
.config(['$routeProvider',function($routeProvider){
$routeProvider.when('/home',{
controller:'SubController',
templateUrl:'templates/home.html'
})
.when('/login',{
controller:'MainController',
templateUrl:'templates/login.html'
});
}])
.controller('MainController',['$scope','$window','userService',function($scope,$window,userService){
$scope.isLoggendIn=userService.isLoggedIn;
console.log('here I come');
$scope.username='';
$scope.password='';
$scope.login=function(){
alert('Ignored???');
$window.localStorage['myUser']={username:$scope.username,password:$scope.password};
consoel.log('user is logged in now');
};
}])
.controller('SubController',['$scope',function($scope){
$scope.myContent='Contenst to show after logged in';
}])
.service('userService',['$window',function($window){
var self=this;
self.isLoggedIn=function(){
if($window.localStorage['myUser'])
return true;
else
return false;
};
}]);
答案 0 :(得分:3)
请更改您的代码。好像你拼错了。
<from ng-submit="submit()">
为:
<form ng-submit="submit()">
同时替换以下代码。
<input type="submit" value="submit" id="submit" ng-click="submit()"></input>
答案 1 :(得分:1)
此地点ng-submit="login()"
不需要type="submit
,最后一个button
不是input
<from ng-submit="submit()">
<input type="text" id="username" ng-model="username"></input>
<input type="password" id="password" ng-model="password"></input>
<button type="submit" value="submit" id="submit"></button>
</form>
答案 2 :(得分:0)
编辑您的代码:
<input type="submit" value="submit" id="submit" ng-submit="login()"></input>
必须如下:
<input type="submit" value="submit" id="submit" ng-click="submit()"></input>
答案 3 :(得分:0)
我认为您的login.html存在问题。删除login()因为你有submit()
<from ng-submit="submit()">
<input type="text" id="username" ng-model="username"></input>
<input type="password" id="password" ng-model="password"></input>
<input type="submit" value="submit" id="submit"></input>
</form>