html - $scope 在 AngularJS 函数中不起作用

标签 html angularjs angularjs-scope

我的代码:

HTML

<body ng-app="loginApp" ng-controller="loginCtrl" > <div class="loginWrap" > <input type="text" placeholder="Username " ng-model="user" /> <input type="text" placeholder="Password " ng-model="pass" /> <input type="button" value="Login" ng-click="checkLogin()" /> <span ng-bind="resultLog"></span> </div> </body>

Angular 代码:::

 var appLog = angular.module('loginApp',[])
 .controller('loginCtrl',function($scope,$http){

 $scope.checkLogin = function(){

  if($scope.user.length > 0 && $scope.name.length > 0  )
  {
      $http.post('php/checkLogin.php',{'n':$scope.user, 'p':$scope.pass})
         .success(function(data)
         {
            if(data)
            {
                $scope.resultLog = "Valid"
            }
             else
            {
                $scope.resultLog = "InValid"
            }
         })
         .error(function()
         {
           $scope.resultLog="Error"
         })
  }
  else
  {
    $scope.resultLog = "Fill All Fields";
  } // end of validation


 } })

我正在学习 Angular JS.. 并执行一些..

问题::$scope.resultLog不工作

作用域对象在函数内部不起作用。 它在 LoginCtrl 中声明并且 我在函数内部使用,在 LoginCtrl 内部声明

提前致谢

最佳答案

在你的 Controller 中,你使用$scope.name,它指向什么?因为你有ng-model="user" >,而不是 ng-model="name"

您的输入并不直接指向user.name,而是指向user。你想这样改变它:

<input type="text" placeholder="Username " ng-model="user.name" />

在你的 Controller 中:

$scope.checkLogin = function(){

  $scope.user = {};    

  if($scope.user.name.length > 0 && $scope.user.name.length > 0  )

  //......

}

关于html - $scope 在 AngularJS 函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22832064/

相关文章:

html - 在可变大小元素的情况下文本重叠

javascript - 在 UIWebView 中更改字体大小后重新定位

javascript - 如果用户单击不同的按钮,则禁用表单元素

angularjs - 将异步获取的数据传递给指令

angularjs - Angular 6 中的 $scope.$apply() 等价物是什么?

javascript - 根据鼠标移动动画 css [我该如何改进]

javascript - 单击按钮时添加 HTML 表单

javascript - AngularJS 从 MongoDB GridFS 播放视频

javascript - 确保文本框仅填充下拉列表中的值。

javascript - Uncaught Error : [$rootScope:infdig]