0

I cannot figure out why the compare function I have and the insert function I have are not working. I believe one part of it might be because the function is never being called by the button? How exactly can I tell that? It does not seem like alerts are working in angular code.

this is my loginHandler.js

(function() {
var app = angular.module('loginHandler', []);

app.controller('LoginController', ['$http' ,function($http){
  var database = this;
  this.validated = false;
  database.dbData = [];

      $http.get('/resources/DatabaseObject.json').success(function(data){
          database.dbData = data;
      });

  this.login = function(credentials){
      loggedInUser = credentals.username;
      validated = true;

      $http.get('/resources/DatabaseObject.json').success(function(data){
          $scope.dbData.push({"123":{"username":"meph","ip":"123","timeLastLogin":"123"}}, "session");
      });
  };

  this.checkSession = function(){
      var ip = "123456";

      if(session.ip.equals(123456)){
          return true;
      }
  };
 }]);
})();

Neither of the functions are working.... Not the login or the checkSession.

DatabaseObject.json

[{
"users": {
    "cra": {
        "firstName": "Jn",
        "lastName": "it",
        "username": "dmph",
        "email": "[email protected]",
        "ip": "192.168.1.1",
        "password": "1234",
        "computerName": "os",
        "prviateKey": "1D3RW12390ASLEWRQ1235"
    },
    "mus": {
        "firstName": "James",
        "lastName": "mh",
        "username": "ch",
        "email": "[email protected]",
        "ip": "192.168.1.1",
        "password": "11212",
        "computerName": "ops",
        "prviateKey": "1D3RW12390ASLEWRQ1235"
    }
},
"session": {
    "123456": {
        "username": "crh",
        "ip": "123456",
        "timeLastLogin": "123456787654"
    }
}
}]

and here is my html

   <html lang="en" ng-app="gemStore">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<title>Basic Company Template for Bootstrap 3</title>

<!-- Bootstrap core CSS -->
<link href="css/bootstrap.css" rel="stylesheet">

<!-- Custom CSS for the 'One Page Wonder' Template -->
<link href="css/one-page-wonder.css" rel="stylesheet">

<script type="text/javascript" src="angular/angular.min.js"></script>

<script type="text/javascript" src="script/headerPanel.js"></script>
<script type="text/javascript" src="script/errorHandler.js"></script>
<script type="text/javascript" src="script/loginHandler.js"></script>
<script type="text/javascript" src="script/sessionHandler.js"></script>
</head>

<body ng-controller="NameController as name">
<div ng-controller="ErrorController as errorController">
<div ng-controller="LoginController as loginCtrl">
<nav class="navbar navbar-fixed-top navbar-inverse" role="navigation">
<div class="container" ng-controller="CredentialsController as credentials">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="index.html">Home</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse navbar-ex1-collapse">
        <ul class="nav navbar-nav">
            <li><a class="act" href="#about">About</a>
            </li>
            <li><a href="#services">Services</a>
            </li>
            <li><a href="#contact">Tutorial</a>
            </li>
            <li><a href="#features">Features</a>
            </li>
            <li><a href="download.html" onclick="">Download</a>
            </li>
        </ul>
        <form>
            <ul class="nas navbar-na">
                <li ng-hide="loginCtrl.checkSession(loginCtrl.dbData.session)"><a>User ID:  <input ng-model="credentials.username" type="text"></a></li>
                <li ng-hide="loginCtrl.checkSession(loginCtrl.dbData.session)"><a>Password:  <input ng-model="credentials.password" type="password" onkeyup="if (event.keyCode == 13) document.getElementById('loginBtn').click()"></a></li>
                <li style="color: red;" ng-show="loginCtrl.checkSession(loginCtrl.dbData.session)"><h2>LOGGED IN AS {{credentials.username}}</h2></li>
            </ul>
            <ul class="nav navbar-nav">
                <li><a ng-click="loginCtrl.login(credentials)" method="post" novalidate type="submit" href="index.html" id="loginBtn">Login</a></li>
            </ul>
        </form>
    </div>

    <!-- /.navbar-collapse -->
</div>
<div style="color: red;" ng-repeat="login in loginCtrl.dbData">
    <h1>{{login.session}}</h1>
</div>
</nav>
</div>
<div style="color: red;" ng-repeat="errorArray in errorController.errors">
    <h1>{{errorArray.error}}</h1>
</div>
</div>


<div class="header-image">

<div class="headline">
    <div class="container">
        <h1>{{name.name}}</h1>
        <h2>The Impossible!</h2>
    </div>
</div>

</div>
</body>
</html>

I am not sure if that submit button is ok or not.... Bah! Sorry.... Im really bad at this stuff! I hope someone can help me better understand it, I have been trying different things without anything working.

http://plnkr.co/edit/zYSFmQSU83FMC9UcXhM4?p=preview

2
  • Please provide a plunker/fiddle with a working (or not working) example.. that would help us answer your question Commented Jun 14, 2014 at 18:49
  • @user3585563: Did you happen to take a look at my answer ? Commented Jun 15, 2014 at 8:22

2 Answers 2

1

The push doesn't work, because you have database.dbData, but trying to push into $scope.dbData (which doesn't exist).

The comparing doesn't work, because you compare against session.ip (which isn't defined anywhere).

Sign up to request clarification or add additional context in comments.

3 Comments

I did look at your answer and changed that, (which might not be updated 100 percent in my plunker) But it implements scope instead. But I did take it out and try it two ways. I tried $scope implementing it, I tried database.dbData and I tried this.dbData and still nothing. I actually dont think it is the method itself tho.... I cant seem to get an alert to work in the method, for some reason my button is not calling the method.... (I think). plnkr.co/edit/zYSFmQSU83FMC9UcXhM4?p=preview
@user3585563: This is a modified plunkr that gets both functions called (just by fixing the issues pointed out in my answer). I am sorry, to tell you that your app is a total mess :) At least try to get your head around what is the difference between using $scope and using this+controller as syntax.
Sweet! Its def calling the methods now! Haha. Yeah.... it is def a mess, I honestly am just learning how things work really. I got a small template and started seeing how things interact. So it is super horrible but Its just a learning how things interact! :) And I will def look into the $scope vrs this, because I am still a little confused on it :). Thanks so much tho! No idea if its pushing the data or not but at least it is calling the methods :). I can at least debug more! Thanks for sticking with me through it!
1

I believe it's because you are using this. instead of $scope.

Also, you aren't passing $scope to your parameters...

app.controller('LoginController', ['$http' ,function($http){

It should be:

app.controller('LoginController', ['$http, $scope' ,function($http, $scope){

And down below you should be using: (not this)

$scope.login = function () { /* ... */ };
$scope.checkSession = function () { /* ... */ };

3 Comments

Look at this example: jsfiddle.net/HzrM7 Open it up in your console. I logged this and $scope. Change all the this. to $scope. and you'll see it works all of a sudden. this would be used more in a Service.
Ahh yeah I see, thanks for the info! That helps a lot. But its still not working :( Lol. Grrrr. Could you take a look at my plunker please? I know im just doing something stupid.... I just cant figure it out still :(. plnkr.co/edit/zYSFmQSU83FMC9UcXhM4?p=preview
@user3585563, mcpDESIGNS: Note that using this in a controller is also possible/reasonable, if you use the controller as approach. On any case, this is not the same as $scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.