0

我对 AngularJS 和 javascript 比较陌生,请在回答时仁慈。

我正在尝试创建一个要嵌套控制器的示例应用程序。MainController 将始终充当父容器,如果用户登录,它将在顶部呈现菜单和用户名。现在我正在检查用户是否存储在 localStorage 中。

每个页面都有自己的控制器,它将执行页面特定的操作。

我被困在 ng-submit 上,为什么它不起作用?

任何帮助表示赞赏。

索引.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>

登录.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;
    };
}]);
4

4 回答 4

3

请更改您的代码。看来你犯了拼写错误。

<from ng-submit="submit()">

至:

<form ng-submit="submit()">

同时替换以下代码。

<input type="submit" value="submit" id="submit" ng-click="submit()"></input>
于 2015-11-25T12:43:32.033 回答
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>
于 2015-11-25T12:45:30.200 回答
0

编辑您的代码:

<input type="submit" value="submit" id="submit" ng-submit="login()"></input>

必须如下:

<input type="submit" value="submit" id="submit" ng-click="submit()"></input>
于 2015-11-25T12:42:45.210 回答
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>
于 2015-11-25T12:43:03.927 回答