javascripthtmlangularjsng-submit

ngSubmit is not triggering function in controller


I am relatively very new to AngularJS and javascript Please be merciful while answering.

I am trying to create one sample application where I want to nest controllers. MainController which will always be there acting as parent container which will render menu and user name on top if user is logged in. For now I am checking if user is stored in localStorage.

Each page will have its own controller which will do page specific things.

I am stuck at ng-submit and why its not working?

Any help is appreciated.

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

Solution

  • Please change your code. Seems you have made spelling mistake.

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

    to:

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

    Also replace the following code.

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