我是angularjs的新手。我在获取文本框的值时遇到了一些问题。它只返回一个空字符串,我认为因为我声明每个模型等于""。这是我的代码:
HTML
<form name="account" ng-submit="update_account(account.$submitted && account.$valid)" novalidate>
<input required placeholder="{{placeholderForName}}" name="name" ng-model="name" type="text">
<input required placeholder="{{placeholderForUname}}" name="uname" ng-model="uname" type="text">
<input required placeholder="{{placeholderForPassw}}" name="passw" ng-model="passw" type="password">
<button type="submit">Save!</button>
</form>
JS
$scope.name = "";
$scope.uname = "";
$scope.passw = "";
$scope.account_update = function(isValid){
if(isValid){
$http.post('query?action=tnoucca_etadpu',{
'name': $scope.name,
'uname': $scope.uname,
'passw': $scope.passw
})
.then(function(response){
alert(response.data); // alerts empty string
})
.catch(function(response){
$scope.showToast("SYS_ERROR: " + response.data);
});
}
};
PHP
if ($action == "tnoucca_etadpu") {
$data = json_decode(file_get_contents("php://input"));
$name = $data->name;
$uname = $data->uname;
$pw = $data->passw;
echo $name;
}
答案 0 :(得分:1)
ng-submit="update_update(account.$submitted && account.$valid)"
而不是$scope.account_update
参见示例plnkr here
您的角度代码没有任何问题。
另外
您可以使用:if($scope.account.$valid){}
代替if(isValid){}
,而不需要传递任何值。