ngSubmit 不是控制器中的触发功能

ngSubmit is not triggering function in controller

我对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;
    };
}]);

编辑您的代码:

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

必须如下:

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

我认为你的 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 ng-submit="submit()">

至:

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

同样替换下面的代码。

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

这里不需要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>