Я относительно новичок в 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;
};
}]);
Измените свой код. Кажется, вы совершили орфографическую ошибку.
<from ng-submit="submit()">
чтобы:
<form ng-submit="submit()">
Также замените следующий код.
<input type="submit" value="submit" id="submit" ng-click="submit()"></input>
Нет необходимости в ng-submit="login()"
в этом месте type="submit
ist достаточно, а последний один не input
button
<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>
Я думаю, что проблема в вашем 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>
<from...
не выглядит правильно для меня.
Редактируйте свой код:
<input type="submit" value="submit" id="submit" ng-submit="login()"></input>
Должно быть:
<input type="submit" value="submit" id="submit" ng-click="submit()"></input>
ng-submit
даже не должно быть, input#type:submit
должен вызывать ng-submit
в форме