2016-11-10 57 views
0

我有一个评估使用angularjs 用户名和密码登录页面,但似乎我在控制器条件语句返回false,即使我把一个真实值如果angularjs语句不工作

,这里是我的html文件

<div class="row"> 
<form action="/"> 

    <input type="text" class="form-control" id="username" ng-model="username" required>      
    <input type="Password" class="form-control" id="username" ng-model="password" required>      
    <br> 
    <button type="button" style="width: 40%;" ng-click="submit()"> Log In </button> 

</form>  
</div> 

和我的控制器

var app = angular.module('myApp', ["ngRoute"]); 

app.controller('ctrl', 
function($scope, $location) { 

    $scope.submit = function() { 
     var username = $scope.username; 
     var password = $scope.password; 

     if($scope.username == 'admin' && $scope.password == 'admin') { 
      console.debug('success'); 
     } else { 
      console.debug('fail'); 
     } 
    } 


}); 
我每次在用户名和密码,我总是得到输入“管理员”在“失败”

我的控制台意味着提交函数返回false。 。

+0

opps类型多数民众赞成在应该是id =“密码”,但我认为这不会导致问题,因为即时通讯不使用id atm如果我没有错 –

+0

你可以让你的问题说明你的问题 – gh9

回答

1

1)每HTML规范,你需要不能有2个不同的元素 html 4.1

2)你应该明确声明你希望你的控制,注入了像下面的对象相同的ID。这样,当你minifiy你的代码,依赖注入将继续工作

app.controller('ctrl',['$scope','$location' 
function($scope, $location) { }]); 

3)回答你的问题,为什么没有这个工作。我的plunkr工程。我删除了路由,因为它不需要。请务必在 '管理员' 没有空格/打字帽

0
<html> 
<head> 
    <script src="angular.js"></script> 
    <script src="route.js"></script> 
</head> 
<body> 
    <div class="row" ng-app="myApp" ng-controller="ctrl"> 
     <form action="/"> 

      <input type="text" class="form-control" id="username" ng-model="username" required>      
      <input type="Password" class="form-control" id="username" ng-model="password" required>      
      <br> 
      <button type="button" style="width: 40%;" ng-click="submit()"> Log In </button> 

     </form>  
    </div> 
    <script> 
     var app = angular.module('myApp',["ngRoute"]); 

     app.controller('ctrl', 
         function($scope, $location) { 

      $scope.submit = function() { 
       var username = $scope.username; 
       var password = $scope.password; 

       if($scope.username == 'admin' && $scope.password == 'admin') { 
        console.debug('success'); 
       } else { 
        console.debug('fail'); 
       } 
      } 
     }); 
    </script> 
</body>