我的代码:
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/